the code above will compile and run ,the output is: " I am S2 I am S1 I am S2 " If I remove the method display from the class S2
but if I remove the method display from the class S2 the output change to : " I am S1 I am S1 I am S1 " A method(display1) is override in subclass,and there is a field.variable in subclass and the super with same name(tt) ,and the method(display1) will invoke the tt(field variable).when the instance call the method(display1) , All that seems that the method always use the field variable which class it reside in. (Marilyn added code tags) [This message has been edited by Marilyn deQueiroz (edited October 28, 2001).]
Hmmm i am not sure if you are worrying the polymophism or the reference side when you made the statement S0 s0 = new S2(); anyway someone (Jane) makes everything clearer to me. 1. the method display() of S2 overriding the display() method of S1, when ever s2.display() will invoke the instance method of s2. 2. if you dont write the display() method at S2 class (ie not overriding it) , instance object of s2 will automaticly use the parent's display() method ( the power of polymorphism ) 3. however when the statement is: S0 s0 = new S2(); s0 is the object reference of type S0 but it is holding an instance object of S2. s0.display() will call the display method of S2 type object. Say reference s0 is an address of 0x0001 so "what is at" that address is an instance of S2 object
I hope that's help and correct me if I am wrong please
Ferry "the only way for you to understand is to make others people can understand your explanation"
subject: It is so confusion Question about polymorphism.