Hi all
I'm executing this code and the result that i'm getting is different than what i was expecting. can some help me with this.
---------------
class ClassRef{
public static void main(String a[]){
Test2 t2 = new Test2();
Test1 t3 = t2;
System.out.println("t2.s: "+ t2.s);
System.out.println("t3.s: "+ t3.s);
t2.method();
t3.method();
}
}
class Test1{
String s ="test1";
void method(){
System.out.println("test1Method");
}
}
class Test2 extends Test1{
String s ="test2";
void method(){
System.out.println("test2Method");
}
}
--------------
o/p is:
t2.s: test2
t3.s: test1
test2Method
test2Method
-------
I was expecting:
t2.s: test2
t3.s: test2
test2Method
test2Method
-------
I'm still thinking that after this line of code
Test1 t3 = t2;
t3 refers to Test2 class instance and should give
t3.s value as "test2".
Thanks