+ 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??

20th Oct 2020, 3:32 AM
Aditya Kumar
Aditya Kumar - avatar
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.
20th Oct 2020, 3:55 AM
Minho
Minho - avatar
20th Oct 2020, 3:56 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Got it!! Thanks Minho đŸ‡°đŸ‡· and ChaoticDawg 😊
20th Oct 2020, 5:44 AM
Aditya Kumar
Aditya Kumar - avatar
+ 2
p.s. you'll be using upcasting a lot once you start learning design patterns.
22nd Oct 2020, 2:05 AM
Shen Bapiro
Shen Bapiro - avatar