final modifier in java | Java Online Tutorials

This Java Online Tutorials “final modifier in java” is providing clear information about final modifier in java with clear general examples to understand the concept easily.


 

final: It restricts extensions(update) on Object.

  • It is a keyword
  • It is a modifier
  • It can be applied to Class, Method & Variable.

 

Class is final :

  • Class can be final.
  • Final class cannot be extended(updated).
  • Only leaf classes(no extension) in the hierarchy must be defined as “final”.

 

Method is final:

  • ‘final’ can be applied to method.
  • final method cannot be overridden.
  • Overriding is the concept of updating functionality of Parent in Child.
  • If we want to restrict only a specific method not to be updated, can be defined as final.

 

Variable is final:

  • Variable can be final.
  • Final variable cannot be modified.
  • Constant variables in java application must be defined as final.
  • Most of the final variables are static in java application.
  • Constant variables(final) are common for all.

 

  • We cannot update the functionality of Object if it is final.
  • But we can access the complete functionality of Object.

 

Can we apply “final” modifier to constructor?

  • We can’t apply final modifier to constructor.
  • ‘final’ modifier restricts object updations.
  • As we are not updating(override) constructor anyway, no need to apply final modifier to constructor.

 

Share this