Home >Java >javaTutorial >super keyword in Java
Use the super keyword when calling the superclass version of an overridden method.
Live Demonstration
class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog { public static void main(String args[]) { Animal b = new Dog(); // Animal reference but Dog object b.move(); // runs the method in Dog class } }
This will produce the following results -
Animals can move Dogs can walk and run
The above is the detailed content of super keyword in Java. For more information, please follow other related articles on the PHP Chinese website!