• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why use overrided version???

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class SuperC
{
public SuperC(){
amethod(); //use the sub's version???
}
public void amethod(){
System.out.println("super's me");
}
}

class SubC extends SuperC
{
public SubC(){
//will call the super()
}
public void amethod(){
super.amethod();
System.out.println("sub's me");
}
public static void main(String[] args)
{
SubC sub = new SubC();
}
}//
//the result shows that the super() calling has used the sub's overrided amethod instead of its own, why???
can someone explain this, thanks!
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output I get when I run the file is:
super's me
sub's me
Which is what I would suspect since the only way you will be able to run the super's overridden method is a call to super.amethod().
Bill
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael
Really this is an interesting question. When the amethod() is invoked, that call is equivalent to this.amethod(). here, this means, the current object. In this case, the current object is of type subC. so, the method in sub class is executed instead of super's.
i hope this is correct....if not please give reply.
thanks
bye

Originally posted by Michael Lin:
class SuperC
{
public SuperC(){
amethod(); //use the sub's version???
}
public void amethod(){
System.out.println("super's me");
}
}

class SubC extends SuperC
{
public SubC(){
//will call the super()
}
public void amethod(){
super.amethod();
System.out.println("sub's me");
}
public static void main(String[] args)
{
SubC sub = new SubC();
}
}//
//the result shows that the super() calling has used the sub's overrided amethod instead of its own, why???
can someone explain this, thanks!


 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now try it with amethod() in SuperC declared as private. And remove the super.amethod() call in SubC's amethod() body. Run it and you will get "super's me". Change access to anything other than private, and you'll get "sub's me". So the version that gets invoked will depend on access level.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The out put is correct. Check this link http://developer.java.sun.com/developer/TechTips/2000/tt1205.html#tip2
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic