Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Problem with calling the right inherited method

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

I am preparing or a SCJP and I have this one anomaly, that I need help in explaining. It is about calling the right inherited method:

So I have three classes:


And I want to execute "new Sub2().callMasterIs();"

So the problem is in callMasterIs() method. There I try to call both Super.getI() and Sub.getI(), but right now I end up calling Sub.getI() two times. How to I invoke the Super.getI() in the Sub2 class?

In short, how do you replace that line:
((Super) this).getI();
So it calls getI() method in Super class.

Thank you.

[ May 14, 2008: Message edited by: Juhan Voolaid ]
[ May 14, 2008: Message edited by: Juhan Voolaid ]
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Juhan Voolaid:

So the problem is in callMasterIs() method. There I try to call both Super.getI() and Sub.getI(), but right now I end up calling Sub.getI() two times. How to I invoke the Super.getI() in the Sub2 class?

In short, how do you replace that line:
((Super) this).getI();
So it calls getI() method in Super class.

Thank you.

[ May 14, 2008: Message edited by: Juhan Voolaid ]

[ May 14, 2008: Message edited by: Juhan Voolaid ][/QB]





When you are invoking Super, it means the super of the current class - that is Sub.
When you are using "this" it again means the object of the current class which again returns you the method of the Sub class.

What you can do is, create an object of the Super class and use it invoke it's method.

Check this code.




Hope it helped.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you are not aware..

Super is a key word used to invoke the method of the parent class.
So even if you are using "Super (this)"
It is not actually casting,you can simply use "this" as it is and you will still get the same result.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Juhan

first lets me tell you that there is no way to invoke Super.getI() method from
Sub2 class.

((Super) this).getI();


Why the Sub.getI() is called? The reason is that a cast only changes the type of the reference (in this case to Super), not the class of the object (which is still Sub2)and method invocation is determined by the class of the current object, resulting the Sub.getI() method being executed.
-----------------------
Khaled
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Juhan,

There's a few issues going on here that I think are contributing to your confusion.

The first is your choice to name your parent class "Super", which may be confusing you about the use of the keyword super. The Java tutorial gives a good introduction to super and how to use it.

Second, because you're trying to experiment with a a third-generation class, Sub2, you're not going to get the behavior you expect. Sub2 inherits from Sub, which overrides Super's implementation of getI with its own.

To access a parent's class method, don't use casting, use the super keyword:



But because Sub2's super class is Sub, it will always call Sub's implementation of getI().

If you're really trying to experiment with casting, then you have to remember effect of polymorphism and late binding -- the version of of the method that is called be called depends on the type of the object that has been created, not on the type of the reference.

To see this, I've slightly modified your code:



If you run this, you'll get the following output:



Hopefully this explanation will get you a little closer to better understanding what's going on here.
 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even if you use differnet variable names insuper abd sub. Things remain same. As the nature of static contributes. Pls. take that also into consideration
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Horry and Stevi for your explanation. And sorry for the confusing "Super" class name. It brought a bit confusion to this discussion aswell.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic