You are here: Home / Topics / Program to explain final class restricts (not allow) in Java

Program to explain final class restricts (not allow) in Java

Filed under: Java on 2023-09-17 07:09:35

// Program to explain final class restricts (not allow) 
// the inheritance from that class.

final class A
{
void display()
{
 System.out.println(" A\'s display().");
}
}

// final class can't be inherited.
// class B extends A // Error
class B
{
void display()
{
 System.out.println(" B\'s display().");
}
}

class  FinalClassTest
{
public static void main( String args[ ] )
{
 A a = new A();
 B b = new B();
 a.display();
 b.display();
}
}


Output:

A's display().
B's display().

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.