I have always this question burning in me. Why do people assign objects of subclass to a superclass reference as below:
What exactly are the benefits and reasons that makes people do so?
public static void main(String[] args)
{
A obj1 = new C(); //Why do this ?
C obj2 = new C(); //When we can simply do this ???
}
class A
{}
class B extends A
{}
class C extends B
{}
I understands all the logic where a Class Dog is an Animal but Animals are not essentially Dogs. I've also bumped into this link: Why assign a subclass object to a superclass reference? which asked similar question as me (but was marked as duplicate with a non-relevant link: What does it mean to "program to an interface"?)
In here: Why assign a subclass object to a superclass reference? Some vague answers were given as reason for doing so. Unfortunately no more detailed answers could be given before it was marked duplicated with an irrelevant link.
Hopefully someone could look into this question and give a detailed reply on the reason of assigning objects of subclass to a superclass reference.