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 ?
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.
+ 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 :)
+ 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.