Overriding Member Methods

Overriding Member Methods

Previous Home Next

 

If a class inherits a method from its super class, then there is a 
chance to override the method provided that it is not marked final.

Member overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.
 


 
class Animal
{

public void move()
{
System.out.println("Animals can move");
}
}

class Cat extends Animal
{

public void move()
{
System.out.println("Cat can walk and run");
}
}

public class Cat1
{

public static void main(String args[])
{
Animal a = new Animal(); // Animal reference and object
Animal b = new cat(); // Animal reference but Cat object

a.move();// runs the method in Animal class

b.move();//Runs the method in Cat class
}


Previous Home Next