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