Benefits of using Final class

24 May 2008

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:

public final class ErrorCodes {
...
}

You will get the following error if you try to inherit a final class:

Inheriting from Final class

del.icio.us:Benefits of using Final class  digg:Benefits of using Final class  spurl:Benefits of using Final class  wists:Benefits of using Final class  simpy:Benefits of using Final class  newsvine:Benefits of using Final class  blinklist:Benefits of using Final class  furl:Benefits of using Final class  reddit:Benefits of using Final class  fark:Benefits of using Final class  blogmarks:Benefits of using Final class  Y!:Benefits of using Final class  smarking:Benefits of using Final class  magnolia:Benefits of using Final class  segnalo:Benefits of using Final class  gifttagging:Benefits of using Final class

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

One Response to “Benefits of using Final class”

  1. Mumbly Joe Says:

    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

Leave a Reply