In java, final methods are declared with final keyword. We can not override the final methods.
Example
class Show{
//final method
public final void show(){
System.out.println("Inside final method");
}
}
class FinalMethodTest extends Show{
//try to override final method
public void show(){
System.out.println("Inside overridden final method");
}
}
public class Main {
public static void main(String args[]){
//creating object of FinalMethodTest Class
FinalMethodTest obj = new FinalMethodTest();
//method call
obj.show();
}
} |
class Show{
//final method
public final void show(){
System.out.println("Inside final method");
}
}
class FinalMethodTest extends Show{
//try to override final method
public void show(){
System.out.println("Inside overridden final method");
}
}
public class Main {
public static void main(String args[]){
//creating object of FinalMethodTest Class
FinalMethodTest obj = new FinalMethodTest();
//method call
obj.show();
}
}
Output
Main.java:9: error: show() in FinalMethodTest cannot override show() in Show
public void show(){
^
overridden method is final
1 error |
Main.java:9: error: show() in FinalMethodTest cannot override show() in Show
public void show(){
^
overridden method is final
1 error
Java interview questions on final keyword