Java Super To Invoke Parent Class Method
Chapter:
Inheritance
Last Updated:
31-07-2017 16:24:09 UTC
Program:
/* ............... START ............... */
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void eat() {
System.out.println("eating bread...");
}
void bark() {
System.out.println("barking...");
}
void work() {
super.eat();
bark();
}
}
class TestSuper2 {
public static void main(String args[]) {
Dog d = new Dog();
d.work();
}
}
/* ............... END ............... */
Output
Notes:
-
Super keyword can be used to invoke the parent class method
- In order to call parent class method you have to use super keyword inside the subclass method like : super.myMethod();.
Tags
Super To Invoke Parent Class Method, Java, Inheritance