Last Updated on April 26, 2023 by Prepbytes
A final class in Java is a concept of object-oriented programming where a class is declared using the "final" keyword. This type of class cannot be extended or inherited by other classes, making it inflexible. In this article, we will discuss the basics of the final class in Java, including how to create them and some examples. We will also explore the usage and advantages of the final class in Java.
But before directly jumping to the final class let’s first understand about the final keyword and how it is relevant to the final class in Java.
Final Keyword in Java
In Java, the "final" keyword is used to indicate that a variable, method, or class can only be assigned a value once and cannot be changed thereafter. The final keyword is used to limit access and modification by the user. It can be used in any context such as final variable, final class, or final method, but here in this article, we will be using this final keyword in the context of the class.
When we use the final keyword in front of the class to make it a final class then we will not be able to extend that final class.
As we know that the final keyword is used to prevent the use of the same thing at different places like to prevent inheritance, overriding, and create a final class, constant variable, and final methods.
Now we are ready to learn the final class in Java.
What is the Final Class in Java
From the above introduction, it is now clear that to make anything final we just have to add a final prefix to it. So to make a final class in Java, add a final keyword in front of the class.
A final class in Java cannot be inherited or extended, meaning that no subclass can be created from it. In other words, there is no subclass you can find which will inherit the final class in Java. If a class is complete in nature then we can make it a final class which tells us that it can’t be an abstract class.
If in case anyone tries to inherit the final class to any subclass, the compiler will throw an error.
In this segment of the article, we learned what is the final class in Java now further in this article, we will learn about the creation of a final class in Java.
How to Create the Final Class in Java
These classes are also known as the last class because no other class can inherit them.
In Java, some classes like String, Integer, and other wrapper classes are also proclaimed as the final class.
To create the final class in java simply add the final keyword as a prefix to the class as shown in the syntax of a class given below –
final class className
{
// Body of class
}
This is how any final class is created in java.
Till now we have learned what is a final class in java and how to create it further in this article we will learn some usage of the final class in java.
When and How to Use the Final Class in Java
Before using final class we should have a deep clarity about what is final class in java, now moving on to the usage of a final class, we can use final class in mainly two ways –
-
To prevent inheritance – as we learned above in the article a final class cannot be extended or inherited.
final class X { // body of class X } class Y extends X { // body of class Y }
This syntax of code will give us a COMPILATION ERROR.
-
To create immutable classes – We use the final keyword to create an immutable class. It means an immutable class can always be a final class.
What will happen if we try to Inherit from a Final Class in Java
Simply the compiler will throw an error because we already know a final class in java cannot be inherited or extended. Let’s now see this with the help of an example given below to make a good understanding of the concept –
Code Implementation of Final Class in Java
import java.util.*; import java.lang.*; import java.io.*; final class finalclass { void method() { System.out.println("this is final class"); } } class childClass extends finalclass { void method() { System.out.println("this is the childClass"); } } class MainClass { public static void main(String arg[]) { finalclass fx = new childClass(); fx.method(); } }
Output:
Main.java:16: error: cannot inherit from final finalclass
class childClass extends finalclass
^
So from the above code output, we can clearly see that it will throw a compilation error because the class is declared as final. This proves that we cannot inherit from the final class in Java.
Advantages of Final Class in Java
The final class in Java has several advantages. Some of them are listed below-
- The final Class in Java provides security as they cannot be inherited by any other classes which means that the classes that are extended may reveal private and protected information about potential users, but with the use of the final class, it won’t happen.
- Data elements of the final class in Java cannot be changed by external access because the final class in java is complete and immutable
- It can be used to prevent a class from being subclassed, which can be useful for security and reliability.
Summary
The summary of the above article is given below in crisp form –
- A class that has the final keyword in its declaration is the final class.
- Inheritance can be prevented with the use of the final class in Java.
- Integers and Strings are a complete class which is the final class.
- The compiler throws an error during compilation if we attempt to inherit from a final class in Java.
- The final Class in java is an immutable class.
- The final class offers some benefits like immutability and security because it cannot be inherited and is immutable.
In this article, we covered the final class in java completed by learning from its creation to its uses and discussed its advantages, and come to the conclusion that it is used to prevent inheritance and implement it by simply adding the final keyword in the prefix.
Frequently Asked Questions(FAQs)
Some commonly asked questions of “Final Class in Java” are given below.
Ques 1. What are the disadvantages of using a final class in Java?
Ans. The main disadvantage of using a final class in java is that it can limit flexibility in the code and make testing more difficult.
Ques 2. What is the difference between a final method and final class in Java?
Ans. A final class in Java cannot be extended, while a final method cannot be overridden.
Ques 3. Can final classes have static methods in Java?
Ans. Yes, final class in Java can have static methods.
Ques 4. Can final classes have private methods in Java?
Ans. Yes, final class in Java can have private methods.