Final Modifiers

The final modifiers

Final modifier is very versatile

  • When the final modifier is applied to any class, it means that the class cannot be subclassed
  • When it is applied to any variable, it means that the variable is constant.
  • When it is applied to any method, it means that the method cannot overridden by sub classes.

FINAL CLASSES

Occassionally, if you want to prevent someone from deriving a class from one of your classes you use the “final” modifier in the definition of the class. Classes that cannot be parent classes are called final classes

Syntax……

public final class abc

{

}

FINAL METHODS

Final methods are methods that cannot be overridden by methods in sub classes. When you declare a method “final” you are saying implementation provided never change. This allows Java compiler to optimise by inlining final methods.

Syntax…….

  1. class abc

{

Public final void get()

{

}

}

FINAL VARIABLES

To declare constants in Java, use final modifier. When applied to a variable, final indicates that the value of the variable will not change.Final variable must be given their value at the time of declaration. Local variable cannot be declared final.

Syntax…..

Class abc

{

private final static char a=” “;

private final static char b=”\t”;

}