The final Keyword in Java

Final Variables:

Variables that are final are prevented from being modified. Once it is declared and initialized, it can’t be changed after that.
final int marks = 95;
final float score = 80.50f;
Ant attempts to modify the above variables will throw a compile-time error.
f1

Points to Remember:

  • Final variables must be initialized during its declaration.

Final Methods:
A method that is declared as final cannot be overridden in a subclass.
public final int marks()
{}

f2

f3

f4

Points to Remember:
marks() cannot be overridden in subclass.
Final Classes:
Classes which are declared final fall under below circumstances.
1. All method inside class cannot be overloaded.
2. Final classes cannot be subclasses which in turn avoids inheritance.
Example:

f5

f6

f7

Leave a Reply