Introduction to Final Class in Java
A class declaration with word-final is known as the final class. Final Class in Java can not be inherited & can not be extended by other classes. A final class can extend other classes; It can be a subclass but not a superclass. Simply, when we need to create an immutable class, then the final class is the best answer. The final class is in itself a declaration that says that it’s final, i.e. this class has no subclass & can not be extended furthermore. The final class keeps some more importance besides immutability.
Sometimes, it is best practice to use the final class for security purposes, As some classes perform security-related things like authentication and authorization. In such a scenario, strings, methods inside of the class should not be modified in the whole life cycle of the application, then making class as final is a better practice.
Making class Final improves the efficiency of the application.
Syntax:
final class Student {
private int age = 3;
}
/* // Trying to inherit the Student class
class Info extends Student{
public static void main(String[] args) {
//creating instance of the HeavyVehicle class
System.out.println(“I am unable to inherit the class with the final keyword”);
}
}
The above-given class with the final word is a final class. If we are going to extend this final class with another, then it will produce errors such as “error: cannot inherit from final Student”, as given in the below screenshot.
How does Final Class work in Java?
When declaring a class with the final keyword, It enables JVM to make assumptions & optimization. As final is the reserved keyword of java, and once added to any class, it means reference for that class is not allowed to be changed. The compiler will check if any referencing is taking place again then it will produce an error.
What happens if we declare any class final & instantiate any final class, then it becomes created in the pool area, objects created in the pool have a special feature of immutability. Some of the best examples of immutable java classes are String, Integer, Double, etc.
If a class is declared as final, then all methods of that class implicitly get declared as final. Final is a modifier in java; final can be applied to a variable, method or classes. Once the modifier was added to the class, all variables & methods of that class would be immutable implicitly. Making class immutable provides several benefits, such as It is read-only & can not be safely shared in multiple threads without any synchronization.
Examples of Final Class in Java
Given are the following examples of Final class in java:
Example #1
In the below-given program, we can see inheritance is not allowed for the HeavyVehicle class because it is a final class.
Code:
//This class will not be extended
final class HeavyVehicle{
void messages(){
System.out.println("Your Vehicle Insurance is going to be expire in the next month");
}
}
// Inheriting HeavyVehicle class is not allowed as it is a final class
/*class Vehicle extends HeavyVehicle{
}
*/
//main class
class Car{
public static void main(String[] args) {
//creating instance of the HeavyVehicle class
HeavyVehicle hvObj = new HeavyVehicle();
hvObj.messages();
}
}
If we try to inherit the HeavyVehicle class, then we will get the following error, as given in the below screenshot.
Now we will move to the next class, where we create an instance of the final class to use its method. Output for the given program after skipping the inheritance process is given below.
Example #2
In this program, the HeavyVehicle class is declared as a final class, so it can not be extended in the other class, so explicitly passing its reference to the Vehicle class method.
Code:
//This class will not be extended
final class HeavyVehicle{
public double getDistanceCanCover(int mileage, double fuelStatus) {
return mileage * fuelStatus;
}
}
class Vehicle{
private int mileage;
private double fuelStatus;
Vehicle(int mileage, double fuelStatus){
this.mileage = mileage;
this.fuelStatus = fuelStatus;
}
public void DisplayDistanceCanCover(HeavyVehicle hvObj) {
double distCanCover = hvObj.getDistanceCanCover(this.mileage, this.fuelStatus);
System.out.println("Distance Vehicle can cover in the available fuel: " + distCanCover);
}
}
//main class
class Car {
public static void main(String[] args) {
//creating instance of the HeavyVehicle class
HeavyVehicle hvObj = new HeavyVehicle();
//creating instance of the Vehicle class
Vehicle vehObj = new Vehicle(30, 3.5);
//calling the method of the
vehObj.DisplayDistanceCanCover(hvObj);
}
}
In the above-given example, we can see how a final class can be used effectively. The output of the above-given program is given below.
Output:
Example #3
In this example, the final class inherits the other class, i.e. Student, but no other classes can extend the Info class because it is a final class.
Code:
class Student{
public void showDetails(String name, String email, int age) {
System.out.println("\nStudent Name : "+ name);
System.out.println("\nStudent Email : "+ email);
System.out.println("\nStudent Age : "+ age);
}
}
// Trying to inherit the Student class
final class Info extends Student{
//main method of java to start program execution
public static void main(String[] args) {
//creating instance of this class
Info infoObj = new Info();
//calling here method of the parent class
infoObj.showDetails("Alex Carry", "alexcarry1079@gmail.com", 40);
}
}
Output:
Conclusion
In the above-given article, we went through the importance of the Final class in java. I also reviewed how the final class is known as an immutable class in java. I noticed some important points related to declaring a final class. In the given an example, we can see how the final class can be used effectively in other classes by passing it through references.
Recommended Articles
This is a guide to Final Class in Java. Here we discuss how does Final Class work in Java? Along with the respective examples. You can also go through our other related articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses