• 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

super applied twice.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to explicitly go up two class levels to access a method?. For example, I have classes Shape, Triangle extends Shape, and RightTriangle extends Triangle. If I have an instance of RightTriangle and want to use, for example the toString() method in Shape from a method in RightTriangle.

I tried super.super.toString() to no avail (error message using BlueJ was <identifier> expected). I cannot find the correct way in my books.

I can go up one step at a time, of course, but that seems less cool to me. If this is the only way, I will be sorely disappointed.

Thanks in advance for any help.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

First of all let consider your case:
you have toString method in shaper class..
In fact you have override the Object version of toString ...! toString method is originally defined in Object class..
when you extend shape in triangle..you get only one toString method ..which is most specific to Triangle class..or you can say that..you got shape version of toString()..!! you again override this method..!!
when you extend triangle in rightTriangle..you get only one version of toString..which is Triangle version..!!
So now there can be two scenario..!!
1) if you do not override..toString in rightT.. class..then you can call it directly..!! or even if you call it by super.toString()..it will call immediate superclass method..!!

2)If you override..toString..then in order to call toString() triangle version..!! then you have to use super..!!
once methods are override we don't have access to higher superclass method..!!
except in the following case

Shape s = new Shape();
s.toString();

This is the only way to call shape version of toString..
or you have to define a method in Triangle version gives a call to shape version of toString..!!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pardon? I am not sure I understand you, Sunny Jain.

You are correct that there is an Object#toString() method, which is a default returning class name - @ sign - hash Code in hexadecimal.

You can use that toString() method in any class by callingor in a Triangle class like thisor similar. So in Triangle it might print

com.jain.sunny.Triangle@1234abcd: sides: 3.00000 3.00000 3.00000

If you extend your Triangle class to create a RightTriangle class, the method will behave polymorphically and print something like this:

com.jain.sunny.RightTriangle@123abcde: sides: 3.00000 4.00000 5.00000

So there might be no need to override the method.

Or you can override it again like thisor similar. So it might print

com.jain.sunny.RightTriangle@12abcdef: sides: 3.00000 4.00000 5.00000: In RightTriangle class

That is what you will get even if you declareRemember polymorphism will give the method which is attached to the instance via its class object, not the version in the superclass you declare.

So that is how you would have to gain access to a superclass of a superclass, and why it might not be necessary in the first place.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i dont know why you did not understand..Let me try to put you in simple words,
What i meant to say:

 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right Sunny Jain. I got your context of 'having a call to super.toString() in the overridden toString() methods of respective classes'.

But as CR pointed out, it was a bit confusing. It seems to be rather! No worries. CR had also helped you in the same way by quoting the same example given by the OP (Original Poster).

In your case, if you simply delegate to "super.toString()", always you will get the same version of toString() printed. Got that? Irrespective of the instance type, it will invoke the BaseClass (A)'s toString() method, which may not really solve the purpose, as each and everyclass has given a flexibility to override the method to include its own meaningful information to be printed.

But as per CR's example, you are not simply delegating to super, but you append your own subclass specific information also. That way, you get the specific information of each individual class as well.

Hope this helps!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been thinking about this question since yesterday, and I think it is even more inappropriate than I first thought to get at "super.super" class members.

If "Triangle" has overridden "Shape" or "Object" members, then "RightTriangle" has no business getting at Shape or Object members other than via Triangle. If RightTriangle can use a "Shape" object in the form it is in Shape, bypassing the overriding in "Triangle", then you can no longer say that a RightTriangle IS-A Triangle. You can only say it is a Shape. It is no "extends" Triangle, if it can use "Shape" members.

I know I am not being clear. Let's try an example.If you could get Triangle#toString() to return what appears in Object, you would be bypassing Shape, then you could no longer say, "a Triangle IS-A Shape."
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are absolutely correct..!!!I was thinking from syntactical point of view...!! But from conceptual point of view..you have given very strong point..!! I think after getting your point in my head..i don't even need to look at the code for verification....!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic