Showing posts with label final. Show all posts
Showing posts with label final. Show all posts

Thursday, 14 January 2016

Difference between final, finally and finalize in java


final -
final is a keyword.
When a variable declared as final should be initialized only once and it cannot be changed.
In case of classes, final classes cannot be extended
In case of methods, final methods cannot be overridden.


finally -
finally is a block
finally is used in exception handling, is used to place code that are important and it will be executed whether the exception is handled or not.The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.


finalize -
finalize is a method.
Before an object is garbage collected, the run time system calls its finalize() method. That means this method used for clean up processing before object is garbage collected

Tuesday, 21 January 2014

Using final with Inheritance

how to use final apply to inheritance.

Using final to Prevent Overriding


While method overriding is one of Java’s most powerful features.To disallow a method from being overridden,specify final as a modifier at the start of its declaration.

INTRODUCING final

In Java class can be declared as final using final keyword. If the final keyword is used in the class declaration the class becomes unable to be sub-classed. No any class can extend the final class i.e. features of a final class can't be inherited. Various classes of Java library are defined as final fro example java.lang.String, java.lang.System etc. If a final class contains the methods then these methods becomes final implicitly. In Java classes are declared as final for the security and efficiency reason. final makes the class immutable.