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:
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:
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:
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.