If we have class B that extends class A, and we have the following code:
A obj = new B();
Is obj variable refers to instance of A or B?
If we have class B that extends class A, and we have the following code:
A obj = new B();
Is obj variable refers to instance of A or B?
Is obj variable refers to instance of A or B?
Both!
If you own a cat, do you own a cat or an animal? -- You can rightfully say you own both!
Paraphrased in code:
Animal a = new Cat();
a
is a variable that holds a reference to a Cat
object (which at the same time is an Animal
object).
So...
...does a
hold a reference to a Cat
? Yes.
...does a
hold a reference to an Animal
? Yes.