Java Final Method In Inheritance

Chapter: Inheritance Last Updated: 02-08-2017 15:48:50 UTC

Program:

            /* ............... START ............... */
                
class Base {
	// final method
	final public void displayMsg() {
		System.out.println("I'm in Base class - displayMsg()");
	}
}

public class JavaFinalMethod extends Base {
	// Overriding the method displayMsg()
	public void displayMsg() {
		System.out.println("I'm in FinalMethod class - displayMsg()");
	}

	public static void main(String[] s) {
		JavaFinalMethod B = new JavaFinalMethod();
		B.displayMsg();
	}
}

                /* ............... END ............... */
        

Output

ERROR in compiling...
    FinalMethod.java:17: displayMsg() in FinalMethod cannot override displayMsg() in Base; 
overridden method is final
public void displayMsg()

Notes:

  • A final method can not override i.e. you can not redefine the function. But final method can be inherited because final keyword restricts the redefinition of the method.

Tags

Final Method In Inheritance, Java, Inheritance

Similar Programs Chapter Last Updated
Java Final Class Inheritance 02-08-2017
Java Multilevel Hierarchical Inheritance Inheritance 01-08-2017
Java Super To Invoke Parent Class Method Inheritance 31-07-2017
Java Super To Invoke Parent Class Constructor Inheritance 30-07-2017
Static And Dynamic Binding In Java Inheritance 24-03-2017
Java Overloaded Method To Print Array Inheritance 03-09-2016
Dynamic Method Dispatch In Java Example Inheritance 19-06-2016
Abstract Class In Java Example Inheritance 20-07-2016
Method Overriding In Java Example Inheritance 20-07-2016
Method Overloading In Java Example Inheritance 20-07-2016
Inheritance And Constructors In Java Inheritance 20-07-2016
Multilevel Inheritance In Java Example Inheritance 21-06-2017
Super() In Java Example Inheritance 14-07-2016
Inheritance In Java Example Inheritance 20-07-2016

1