When dealing with classes & inheritance remember the following:
It is important to understand the difference between
(a) invoking a method on an object AND
(b) accessing instance variables of an object.
The first is
dynamic lookup . It means that when you call a METHOD using the object reference, then it is the
class of the object that will determine the method executed.
The second is
variable shadowing . This means that whenever a VARIABLE is accessed using the object reference then it is the
type of the reference that will determine the variable accessed.
So let's consider your code
Test1 t3 = t2;
Here the
reference type is Test1
But the
class of the object is Test2 created in previous statement new Test2();
All you have done is assign object of type Test2 to reference of Test3. This is legal because Test2 is a subclass of Test3.
t3.method() will call METHOD in Test2
dynamic lookup because object belong to class Test2 And t3.s will access VARIABLE String s in Test1
variable shadowing because reference t3 is of type Test1 You could play around with these concepts by writing some code. It will help you understand.
regards,
Jyotsna