Benefits of using Final class
24 May 2008Final classes have a lot of benefits if used where required. I will present benefits of final classes in this post.
Sometimes, you wish that no one can inherit from your class since its functionality is completed and should not be inherited. In that case, you may define your class as final, and then no class can inherit from it. The method of final class are also final. Final methods cannot be overridden in any sub class. There is also performance issues related to final class. A final method will run faster than a normal standard method.
Actually Java compiler may be able to inline a final method which enhances the performance. Lets see how to declare a final class:
public final class ErrorCodes { ... }
You will get the following error if you try to inherit a final class:
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Benefits of using Final class
A final method will run faster than a normal standard method.
That is 100% false.
“Just because class X is compiled against final class Y doesn’t mean that the same version of class Y will be loaded at run time. So the compiler cannot inline such cross-class method calls safely, final or not. ”
From: http://www.ibm.com/developerworks/java/library/j-jtp1029.html