| By Yakov Fain | Article Rating: |
|
| June 7, 2010 11:25 AM EDT | Reads: |
1,051 |
If you create classes that may be used by other developers, declaring methods as final will make them not overridable in the subclasses. While today, it may seem obvious to you that a particular method will never ever need to be overridden, you might not properly predict all use-patterns of this class. If this happens, some other developer will have to jump through the hoops to create another version of such a method in a subclass. If you don't want to be cursed in the future, think twice if you really really want to declare this method as final. Do you see any benefits in using final methods?
We had to extend a third party library to improve their implementation of a certain networking protocol. As it usually happens, the code was poorly documented, so we had to read the code to find out which method to override in the subclass. Sure enough, that method was declared as final. We found a workaround and still replaced the call to the final method to the call to our own. So what the original developer achieved by using final? He made our work more difficult than it should have been.
Originally, Java compiler was optimizing (inlining) final methods. Today I've learned (thank you, Heinz) that Java compiler doesn't do it anymore, and they are optimized by the Hotspot JVM:
http://www.javaspecialists.eu/archive/Issue157.html
http://www.javaspecialists.eu/archive/Issue158.html
Who are these guys we're protecting from by using final? I also believe that the keyword protected is equally useless.
Published June 7, 2010 Reads 1,051
Copyright © 2010 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Yakov Fain
Yakov Fain is a Managing Director of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. He is an Adobe Certified Flex Instructor. Yakov co-athored the O'Reilly book "Enterprise Application Development with Flex". He twits at twitter.com/yfain.
- The Next Chapter in the Virtualization Story Begins
- Unveiling the java.lang.Out OfMemoryError
- Is MySQL Doomed to Extinction?
- Gosling Out of Oracle
- Gosling Hints He Left Oracle over Money
- 101 on jQuery Selector Performance
- Leveraging Public Clouds to SaaS-Enable Enterprise Applications
- Bravo, Google!
- It's the Java vs. C++ Shootout Revisited!
- Larry Says What Everyone Thinks About Sun
- Learning JSF2: AJAX in JSF
- CSC to Sponsor and Present at All Cloud Expo Events in 2010
- An Exclusive Interview with Oracle, Cloud Expo 2010 Diamond Sponsor
- The Next Chapter in the Virtualization Story Begins
- Unveiling the java.lang.Out OfMemoryError
- Cloud Computing Bootcamp Returns to Cloud Expo in New York April 20, 2010
- Is MySQL Doomed to Extinction?
- Gosling Out of Oracle
- Gosling Hints He Left Oracle over Money
- 101 on jQuery Selector Performance
- Leveraging Public Clouds to SaaS-Enable Enterprise Applications
- Bravo, Google!
- Novell Owns Unix
- Today iPad is the Only Whore in Town
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?

































