In java, a class declared with final keyword is called as final class. Inheritance is not possible with final class.
Example
final class Show{
//final class method
public void show(){
System.out.println("Inside final method");
}
}
class FinalClassTest extends Show{
public void show(){
System.out.println("Inside overridden method of final class");
}
}
public class Main {
public static void main(String args[]){
//creating object of FinalClassTest Class
FinalClassTest obj = new FinalClassTest();
//method call
obj.show();
}
} |
final class Show{
//final class method
public void show(){
System.out.println("Inside final method");
}
}
class FinalClassTest extends Show{
public void show(){
System.out.println("Inside overridden method of final class");
}
}
public class Main {
public static void main(String args[]){
//creating object of FinalClassTest Class
FinalClassTest obj = new FinalClassTest();
//method call
obj.show();
}
}
Output
Main.java:7: error: cannot inherit from final Show
class FinalClassTest extends Show{
^
1 error |
Main.java:7: error: cannot inherit from final Show
class FinalClassTest extends Show{
^
1 error
Java interview questions on final keyword