final in Java

final Makes it impossible to extend a class, override a method, or reinitialize a variable.

Final Classes When used in a class declaration, the final keyword means
the class can’t be subclassed. In other words, no other class can ever extend (inherit
from) a final class, and any attempts to do so will give you a compiler error.
You should make a final class only if you need an absolute guarantee that none of the methods in that class will ever be overridden. If you’re deeply dependent on the implementations of certain methods, then using
final gives you the security that nobody can change the implementation out from under you.

Final Methods The final keyword prevents a method from being overridden in a subclass, and is often used to enforce the API functionality of a method.

Final Variables Declaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialized with an explicit value. For primitives, this means that once the variable is assigned a value, the value can’t be altered. 
 For example, if you assign 10 to the int variable x, then x is going to stay 10, forever. So that’s
straightforward for primitives, and for final object reference- A reference variable marked final can’t ever be reassigned to refer to a different object.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...