Java Final Keyword

Imran Khan
2 min readAug 27, 2022

--

It is a java keyword used as a non-access modifier with Classes, attributes and methods.

After making class as final will not allow us to inherit the class.

Method as final will not allow us override in child class.

Making any variable as final will not allow us to override the value.

Final variable:

When the final keyword is used with any variable, its value can never be changed in the same or derived class.

In the below code snippet, on line number 2, the number is initialized with the value 99 and defined as final. Reassigning a value on line number 5 will give an error “cannot assign a value to the final variable”.

OUTPUT:

Final method:

The method cannot be overridden once declared as final.

In the below code snippet, on line number 4, the function declared with the final keyword will not be eligible to override in the child class at line number 11. It will give the below inline error as shown under the output section.

OUTPUT:

Final Class:

Class cannot be extends or inherit as subclass once declared as final.

In the below code snippet, on line number 4, the class declared with the final keyword will not be eligible to get inherit in the child class at line number 5. It will give the below inline error (cannot inherit from final class_name) as shown under the output section.

OUTPUT:

I hope you found out this article interesting and informative. Please share it with your friends to spread the knowledge.

You can follow me for upcoming blogs follow.
Thank you!

--

--