• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

It is so confusion Question about polymorphism.

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gong,
Correct.
Methods can be overridden variables can't.
Variables can however be shadowed, as your example shows.
Regards,
Manfred.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this helps... http://www.javaranch.com/ubb/Forum24/HTML/012379.html
Regards,
Hassan.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic