Assume that Animal is a class that has method void info() that prints "I am an animal". Classes Bird and Reptile both extend class Animal, and both override method info(). Class Bird implements method info to print "I am a bird", and class Reptile implements method info to print "I am a reptile ". Determine the outcome printed by the following lines of code.
Animal animal = new Animal();
animal.info(); //OUTCOME:
animal = new Reptile()
animal.info(); //OUTCOME:
animal = new Bird();
animal.info(); //OUTCOME:
When you will execute this code, your output will be like this,
Executing Line no. 2nd of above code, output of info method will be : I am an animal
Executing Line no. 4th of above code, output of info method will be : I am a reptile
Executing Line no. 6th of above code, output of info method will be : I am a bird
Explanation:
In First line of code you are creating a object animal of type Animal.
In 2nd line you are calling info method of animal which will call info method of Animal class as we have created and initialized animal with type of Animal. Quite simple.
Now, in 3rd line we are creating object of Reptile and assigning that object to animal which we have already created.
So, at 4th line by calling animal.info(), it will refer to the method ifo which is of class Reptile that's why it will print "I am a reptile" .
Same thing happens in case of Bird.
So Basically, this mechanism is nothing but Overriding which is very important aspect in inheritance. Overriding means that if we have one class named A which have method print(). And if we create one class named B which inherits class A means, Now class B have also print() method. till now nothing complecated it means if you calling print() method with class B's object, it will give same output as class A's print() method. But if you are providing some different defination of print() method in class B which is child class of class A then things might change. This is the case of Overriding. so, Overriding means that you have same name method in both parent and child class and providing different diffrent defination in both class for that same method. and now if we try to call print() methos using class B's object it will give output as per defination of print() method in class B. it means that method of child class is overriding the method of Parent class. in Overriding you can not call method of parent class using object reference of child class. it always referes child class's method.
Still you want to acess parent class method, then you have to create object of parent class in child class and call method of parent class in method of child class. For exmaple, if you want to call print() of class A then you have to create object of class A inside class B and you have to call print() of class A inside print() of class B using that object. but that means that whenever you call print() of class B it will call print() of class A as we are calling print() of class A from print() of class B.
Get Answers For Free
Most questions answered within 1 hours.