Overriding Member Methods (The super Variable)
Previous | Home | Next |
When invoking a superclass version of an overridden method the super keyword is used.
The call super.method() will invoke the method of immediate super class.
class Animal
{
public void move()
{
System.out.println("Animals can move");
}
}
class Cat extends Animal
{
public void move()
{
super.move(); // invokes the super class method
System.out.println("Cats can walk and run");
}
}
public class Cat1
{
public static void main(String args[]){
Animal b = new Cat(); // Animal reference but Cat object
b.move();//Runs the method in Cat class
}
}
Previous | Home | Next |