What is Final Class in Java?

The final class in java is an OOPS concept which is a class, declared by adding the final keyword in front of the class.
Classes with the final keyword in java cannot be inherited or extended by the other classes. So, it means the final class in java is less flexible or rigid in nature.

In this article, we will learn about the final class in java, how to create a final class in java, and some examples of it.

Later on, we will further learn how and when to use the final class in java and discuss some advantages of the final class in java.

But before directly jumping to the final class let’s first understand about final keyword and how it is relevant to the final class in java.

Introduction to Final Keyword in Java:

In Java, the "final" keyword is used to describe entities that can only be assigned only once and should not change again. The final keyword is used to restrict 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.

The final class is a class that cannot be inherited or extended. 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 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 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 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 –

  1. 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.

  2. To create immutable classes – the final keyword is compulsory 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:

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 which means we cannot inherit from the final class.

Advantages of Final Class in Java:

The final keyword in Java has several advantages. Some of them are listed below-

  • Security – Because it cannot be inherited by any other classes, the final class offers security in java.
  • 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 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.
  • 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.
  • We saw in the article the two uses of the final class.
  • Immutable classes are always the final 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.

Leave a Reply

Your email address will not be published. Required fields are marked *