0

Let's assume we have a case of Multi-level inheritance over here, where there is an 'Animal' super class, and a 'Dog' as a subclass. Now i'll mention the code here which is supposed to involve method overriding. Both the Animal class and the class 'Dog' have the common method called 'eat()'. So my question is if i want to access the eat() function of the class 'Dog', then the syntax should be as follows:-

public class Test
{
public static void main(String args[])
{
Dog obj = new Dog();
obj.eat();
}
}

But instead of this, the book mentions to use the super class during creation of the object. and the actual code looks somewhat like this:-

public class Test
{
public static void main(String args[])
{
Animal obj = new Dog();
obj.eat();
}
}

So, what's the point in using the superclass for creation of the object, when using the subclass for creation of the object works perfectly fine?

I tried to figure out the reason, but couldn't fine anything specific enought to satite my queries. Thanks in advance!

0 Answers0