View RSS Feed

Java Object

What is overriding

Rate this Entry
by , 04-26-2012 at 06:54 PM (1114 Views)
A method is sometimes inherited by a subclass from superclass which is not according the requirements. Perhaps, twenty methods are inherited by subclass & one among them is not right.
Subclass in such case will be overriding those methods, by method redefinition.
Method that is defined in superclass is not affected by this override.

In following example, Cat class is the subclass & superclass includes Animal. Eat() method that has been inherited by the Animal class is overridden by the Cat class.

Java Code:
public class Animal {

    public void eat() {
        System.out.println("Eat for Animal");
    }
}

public class Cat extends Animal {

    public void eat() {
        System.out.println("Eat for Cat");
    }
}

Submit "What is overriding" to Facebook Submit "What is overriding" to Digg Submit "What is overriding" to del.icio.us Submit "What is overriding" to StumbleUpon Submit "What is overriding" to Google

Comments