Java

What is the “final” Keyword in Java?

While programming in Java, there can be instances where the developer needs to restrict from overriding some specific functionalities. For instance, securing or encrypting the data or when there is a need to store the same value, always. In such cases, the “final” keyword in Java enables the developer to make the data confidential.

This blog will elaborate on the usage of the “final” keyword in Java.

What is the “final” Keyword in Java?

The “final” keyword in Java is utilized to restrict the user from overwriting a value. It works such that if a variable or a function is allocated as final, its value cannot be overwritten.

The Java “final” keyword can be utilized in many contexts:

Example 1: Utilization of “final” Keyword With Variable

In this example, the “final” keyword can be associated with a variable and overwritten as well to analyze its usage:

final int score = 35;

score = 45;

System.out.println(score);

In the above code snippet:

  • Firstly, initialize the stated integer value and associate the “final” keyword with it to make its value unchangeable.
  • In the next step, overwrite the integer with another value and display it.
  • This will result in displaying an error since the value associated with the discussed keyword cannot be overwritten.

Output

In the above output, the encountered exception indicates that the value associated with the “final” keyword cannot be overwritten.

Example 2: Utilization of “final” Keyword With Function

In this illustration, the discussed keyword can be utilized with an accumulated function in the class:

classparent{
 public final void out() {
System.out.println("This is the default function");
}}
classchildextendsparent{
 public void out() {
System.out.println("This is an overridden function");  
}}
child obj = new child();
obj.out();

In the above lines of code:

  • Firstly, define a parent class named “parent”.
  • Within the class, define a function named “out()” associated with the “final” keyword and display the stated message.
  • After that, create a child class named “child” inheriting the parent class with the help of the “extends” keyword.
  • In this class, declare the function “out()” identical to the inherited class function displaying the given message.
  • In main, create an object of the “child” class and invoke the stated identical function.

Output

The faced limitation in the above output signifies that the identical function cannot be overridden.

Example 3: Utilization of “final” Keyword With Class

In this particular example, a class can be allocated as “final” and then can be verified by inheriting it by its child class:

finalclassparent{
public final void out1() {
System.out.println("This is the parent class");
}}
classchildextendsparent{
public void out2() {
System.out.println("This is a child class");   
}}
child obj = new child();
obj.out1();

Apply the below-stated steps as given in the above lines of code:

  • Firstly, define a class named “parent” associated with the “final” keyword to refrain from being inherited.
  • Within the class, define the provided function and display the given message.
  • After that, initialize the child class “child” inheriting the parent class via the “extends” keyword.
  • In this class, likewise declare a function named “out2()” and print the stated message in its(function) definition.
  • Lastly, in main, create an object of the child class and invoke the parent class function “out1()”.
  • This will log an error since the class allocated as final cannot be inherited.

Output

In this output, it can be seen that an exception is thrown since the parent class function can not be invoked by the child class.

Conclusion

The “final” keyword in Java is utilized to refrain the user from overwriting a value. This keyword can be associated with a variable, function or class, etc. Upon modifying or inheriting (in the case of class) its value, it logs an error. This article discussed the usage of the final keyword in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.