aspose file tools*
The moose likes Beginning Java and the fly likes super applied twice. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super applied twice." Watch "super applied twice." New topic
Author

super applied twice.

Steven Coddington
Greenhorn

Joined: Oct 17, 2004
Posts: 10
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.


Steve Coddington
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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..!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,
i dont know why you did not understand..Let me try to put you in simple words,
What i meant to say:

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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."
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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....!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super applied twice.
 
Similar Threads
Marcus Green Exam 1, Q
Polymorphic method declarations
How interfaces are used for code reusability
Generic methods and wildcards
polymorphism