I can't understand the meaning of the Yellow information rectangle | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't understand the meaning of the Yellow information rectangle

"This demonstrates that you can use the Animal variable (without actually knowing that it contains an object of the subclass)". I understood the rest of the lesson but this makes absolutely no sense for me. Can someone rephrase it or explain it please ?

18th Apr 2020, 7:59 PM
مروان سالم بامطرف
مروان سالم بامطرف - avatar
3 Answers
+ 2
Do add the reference when asking questions from the course or quiz. https://www.sololearn.com/learn/Java/2164/ This is how atleast runtime polymorphism works. You can create a reference variable of parent class and point it to an object of the child class. Every time the overridden method by the child class will be called under such scenario.
18th Apr 2020, 8:09 PM
Avinesh
Avinesh - avatar
+ 2
Hello مروان سالم بامطرف Maybe this example helps you a bit to understand why you should need this: Imagine you have such method: public void feed(Animal a){ //feed the animal } If you want you can imagine this method belongs to a class Zoo or maybe a class PetShop. Now you can create any kind of Animals. Animal dog = new Dog(); Animal tiger = new Tiger(); and so on... With all of these objects you can call feed(): feed(Tiger); feed(Dog); If somone else want to use this method he just need to make sure that his kind of animal extends the class Animal. The type itself does not matter. I hope it helps a bit :)
18th Apr 2020, 10:00 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Hello مروان سالم بامطرف On Q&A the relation to the lesson is lost. Therefore it fits much better there in the comment part. Please ask there for help.
18th Apr 2020, 8:11 PM
Oma Falk
Oma Falk - avatar