1. In java, the final keyword can be used with 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 that is 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 class. We can
create a class as a final class only if it is complete in nature it means it must not be an abstract
class. In java, all the wrapper classes are final class 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 use of final keyword during the declaration of the class. We must
need to take the class should be complete. It should not an abstract class.
accessModifier final class className
{
// Body of class
}
Here accessModifierspecifies 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 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[])
2. {
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)
Advantage 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 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 could inherit it. If
you try to compile a subclass that inheriting a final class, then the compiler will show an error at
compile time.
To make 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?
1. A final class is introduced in JDK to prevent the inheritance of class. Suppose I have a class
that having some personal or secured information and you want that class should not be extended
by any 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.