In Java, the final keyword can be used with a variable, method, or class. We have already discussed the final variable and final method in java. In this post, we will discuss what is a final class, and how to use it. What is the need for the final class in java?
Here is the table content of the article will we will cover this topic.
1. What is the final class in Java?
2. How to create a final class?
3. Advantages of the final class?
4. When to use a final class?
What is the final class in Java?
A class declared with the final keyword is known as the final class. A final class can’t be inherited by subclasses. By use of the final class, we can restrict the inheritance of the class. We can create a class as a final class only if it is complete in nature, which means it must not be an abstract class. In java, all the wrapper classes are final classes like String, Integer, etc.
If we try to inherit a final class, then the compiler throws an error at compilation time. We can’t create a class as immutable without the final class.
How to create it?
We can create a final class by using the final keyword during the declaration of the class. We just need to take the class should be complete. It should not an abstract class.
accessModifier final class className { // Body of class }
Here accessModifier specifies the scope of the method. You can read about access modifiers.
final is a keyword that is used to make a final method.
returnType is the type of data that you want to return from the method.
className is the name of the class.
final class ParentClass { void showData() { System.out.println("This is a method of final Parent class"); } } //It will throw compilation error class ChildClass extends ParentClass { void showData() { System.out.println("This is a method of Child class"); } } class MainClass { public static void main(String arg[]) { ParentClass obj = new ChildClass(); obj.showData(); } }
Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from ChildClass to ParentClass at MainClass.main(MainClass.java:21)
Advantages of the final class
To provide Security: We can protect sensitive information from unauthorized access. Suppose we have a non-final class that has some sensitive data that should be accessed by only authorized persons. A hacker can inherit that class in the subclass and can make a substitute for it. Now subclass has the methods of our class and it looks and feels like the original class. It can damage our sensitive data, so we can prevent this and provide security to our class.
To prevent this, we can declare the class with the final keyword so that no one can inherit it. If you try to compile a subclass that inherits a final class, then the compiler will show an error at compile time.
To make an Immutable class: To create an immutable class then the final keyword is mandatory. It means an immutable class is always a final class.
When to use a final class in java?
1. A final class is introduced in JDK to prevent the inheritance of class. Suppose I have a class that has some personal or secured information and you want any class should not extend that class. Then you should use the final keyword with class.
2. To create an immutable class then the final keyword is mandatory. It means an immutable class is always a final class.
You written everything around final in single article, really appreciable
It is hard to find any where else
Love your efforts ❤️
Good explaination of final class 👍🏻🙌🙌
nice explanation