+ 2
[Java] super class and subclass question
True or false: Super class reference variables can always point to subclass objects. Ans: True Can someone please explain this statement in simpler words if possible??
4 Answers
+ 6
A superclass reference variable can refer the instance of Child class, it is known as upcasting. e.g.
class Animal{}
class Dog extends Animal{}
In java, we can write
Animal a = new Dog(); // Upcasting
For more info đ
https://www.geeksforgeeks.org/referencing-subclass-objects-subclass-vs-superclass-reference/#:~:text=First%20approach%20(Referencing%20using%20Superclass,method%20that%20will%20be%20executed.
+ 5
Review polymorphism
https://www.sololearn.com/learn/Java/2164/
+ 4
Got it!! Thanks Minho đ°đ· and ChaoticDawg đ
+ 2
p.s. you'll be using upcasting a lot once you start learning design patterns.