
How to use overriding method in the java progrem?

The overriding method are define in the subclass that has same name,same argument and same return type as a method in the superclass.
class Animal {
public void eat(String str) {
System.out.println("Eating for grass");
}
}
class Cat extends Animal {
public void eat(String str) {
System.out.println("Drinking for milk");
}
}
class Dog extends Animal {
public void eat(String str) {
System.out.println("Eating for bread");
}
}
class Goat extends Animal {
public void eat(String str) {
System.out.println("");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
Cat b = new Cat();
Dog c = new Dog();
Goat d = new Goat();
a.eat("grass");
b.eat("milk");
c.eat("bread");
d.eat("grass");
}
}
Output:
Eating for grass Drinking for milk Eating for bread
Description:- The above code demonstrates you the overriding method.we have created a class named Animal, Animal are the supper class .we have seen that a method defined in the supper class is inherited by its subclass and is used by the objects created bye the subclass.now define a method in the subclass that has the same name, same arguments and same return type as a method in the superclass.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.