View RSS Feed

My Java Tips

Benefits of using Final class

Rate this Entry
by , 11-06-2011 at 05:58 PM (1715 Views)
Final 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:

Java Code:
public final class ErrorCodes {
...
}
You will get the following error if you try to inherit a final class:

Inheriting from Final class

Submit "Benefits of using Final class" to Facebook Submit "Benefits of using Final class" to Digg Submit "Benefits of using Final class" to del.icio.us Submit "Benefits of using Final class" to StumbleUpon Submit "Benefits of using Final class" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments