Write a Java program to demonstrates the use of a final class in inheritance


In this Java program, you've defined a final class called FinalClass, and then you've attempted to create a class named DerivedClass that extends FinalClass. However, this will result in a compilation error because you cannot inherit from a final class.

The final keyword, when applied to a class, indicates that the class cannot be subclassed or extended. Therefore, any attempt to create a subclass (like DerivedClass) from a final class (FinalClass) will lead to a compilation error.


Source Code

final class FinalClass
{
    // Some final class members
}
 
// This line will result in a compilation error since FinalClass is final
class DerivedClass extends FinalClass
{
    // Some code here
}

Output

Error : cannot inherit from final FinalClass