Final Class in Java


When used in a class declaration, the final modifier prevents other classes from being declared that extend the class. A final class is a "leaf" class in the inheritance class hierarchy.

Example :
   // This declares a final class
   final class MyFinalClass
   {
       // statements
    }
    // Compilation error: cannot inherit from final MyFinalClass
   class MySubClass extends MyFinalClass
   {
      // statements
   }

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();
        o.display();
 
    }
}
 

Output

I am Display
To download raw file Click Here

Basic Programs