Final Class in Java

In Java, a final class is a class that cannot be subclassed or extended by any other class. When a class is declared final, it means that its implementation is complete and cannot be modified by any other class.

Here's an example of a final class in Java:

   final public class MyFinalClass
       // class members and methods

The program demonstrates the use of the final keyword in defining a class.

In the program, the final keyword is used to declare the finalClassDemo class, which makes it impossible to extend or subclass the finalClassDemo class. The class has a method display() which simply prints a message.

In the main() method, an object o of the finalClassDemo class is created, and the display() method is called on it. Since the finalClassDemo class is not meant to be extended or subclassed, this program successfully prints the message "I am Display".

Thus, the final keyword can be used to prevent inheritance and method overriding for a class.

Source Code

//Final Class in Java
final class finalClassDemo
    public void display()
        System.out.println("I am Display");
public class finalClasss{
    public static void main(String[] args) {
        finalClassDemo o =new finalClassDemo();


I am Display
To download raw file Click Here

Basic Programs