Final Class in Java
Learn via video course

Overview
The final class is a class that is declared with the final keyword. Subclasses can't inherit a final class or a final class cannot be inherited by any subclass. So, we can restrict class inheritance by making use of a final class.
Scope
This article covers the below points
- This article introduces the final Class in Java
- This article shows how to create Final class?
- This article shows how the implementation of the final class
Introduction to Final Class in Java
As the name suggests, Final Class in Java uses the final keyword for its declaration. The final keyword in java is used to restrict the user, similarly, the final class means that the class cannot be extended. We can only create a final class if it is complete in nature, which means it cannot be an abstract class. All wrapper classes in Java are final classes, such as String, Integer, etc. Final class cannot be inherited by any subclass,
If we try to inherit a final class, then the compiler throws an error during compilation.
How to Create Final Classes?
We can use Java's final keyword to create a final class, the definition of the class should be complete and should not be an abstract class.
Syntax:
where final is the keyword used to declare the final class and className is the name of the class we are defining.
Example 1: How to Use final class?
We can simply define a final class using the final keyword and can write the class body code according to our needs.
In the below code, we are creating a final class named myFinalClass which has a method myFinalMethod() that prints some text. So for using our final class we are just creating an instance of our final class in our mainClass, now we are just calling our method myFinalMethod() from the class instance which prints the final output of our code.
Output:
Simply the print statement present inside myFinalMethod is executed.
Example: 2 What happens if we try to inherit from a final Class?
In the below code, we are using the extend keyword to inherit the final class to the subClass but the final class cannot be inherited by any other subclass so it will throw an error saying "cannot inherit from final myFinalClass"
Output:
We got an error when we tried to inherit from our final class
Advantage of the Final Class
- Final class provides security as it cannot be extended or inherited by any other class, classes that can be extended can leak sensitive data of potential users but final classes make data elements safe and secure.
- Final class is a complete and immutable class, so data elements do not change by external access.
Conclusion
- The final class is a class that is declared with the final keyword.
- We can restrict class inheritance by making use of the final class.
- Final classes cannot be extended or inherited.
- If we try to inherit a final class, then the compiler throws an error during compilation.
- We can simply define a final class using the final keyword and can write the class body code according to our needs.
- As the final class is immutable and can't be inherited, it has some advantages such as immutability and security.