Java final Keyword:

  • The final keyword in Java is used to restrict the user.
  • It is used to create a variable in the form of continuous, prohibiting the inheritance, restricting the inheritance override. It is used at variable level, method level and class level.

 

Java final keyword can be used with: 

  1. final variable
  2. final method
  3. final class

 

final variable:

  • When a variable is declared with the final keyword, its value can not be modified, essentially, continuously.
  • It also means that you should start the final variable.
  • If the last variable is a reference, then it means that the variable can not be re-bound to refer to any other object.
  • Internal position of the object pointed in the reference context can change, i.e. you can use the last array or the last Can add or remove elements from the collection.
  • All uppercase is a good practice to represent the last variable, underlining different words.

Example:

Output:

Note:  We got a compilation error in the above program because we tried to change the value of a final variable.

 

final method: 

  • A method declared in the final form can not be overridden.
  • This means that when a child class can call the final method of parent class without any problems, it will not be possible to override it using java final keyword.

Example:

Output:

 

final class: 

  • The class with the final keyword is known as the final class in Java.
  • The final class is completely in nature and can not be inherited.
  • Many classes in Java are final, integer and other wrapper squares.
  • The main purpose or purpose of using the ultimate objective is to prevent the class from being a sub-class.
  • If a class is marked as final, then no class can get any facility from the final class.

Example:

Output: