"super" question (Mughal 6.6)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Given classes A, B and C, where B extends A and C extends B and where all classes implement the instance method, void doIt(). How can the doIt() method in A be called from an instance method in C?
Select the one right answer.
(a) doIt();
(b) super.doIt();
(c) super.super.doIt();
(d) this.super.doIt();
(e) A.this.doIt();
(f) ((A)this).doIt();
(g) It is not possible.
The book says answer g is correct, saying that you can't chain supers, and that if you cast the this reference to A you could only access instance variables not methods.
But I think it is possible, even though none of the displayed answers are correct. You can call A's doIt() from the instance method in C by creating a new instance of A and then using that reference to call the method. I was able to do this in test code, so I know it works. Is there some wording in the question that I'm not understanding properly?
Any help would be appreciated.
Thanks.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Well, the answer is NO. Because whatever Typecasting you may do,
the method call will be from the 'C' class only. However, you may call the <code>doIt() </code> of the "B" class by
a call to super in the A's <code>doIt()</code> method.
Hope this clarifies the issue.
Ravindra Mohan.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Ravindra Mohan:
Hi Micah,
Well, the answer is NO. Because whatever Typecasting you may do,
the method call will be from the 'C' class only. However, you may call the <code>[b]doIt() </code> of the "B" class by
a call to super in the A's <code>doIt()</code> method.
Hope this clarifies the issue.
Ravindra Mohan.[/B]
Ravindra,
You said, "However, you may call the <code>doIt() </code> of the "B" class by a call to super in the A's <code>doIt()</code> method."
I am not quite sure of what you mean! Could you give an example?
Regards,
- Lam -
[This message has been edited by Lam Thai (edited May 09, 2001).]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Micah Holroyd:
For this question:
But I think it is possible, even though none of the displayed answers are correct.
Thanks.
Hi Micah,
You are right! it is possible to invoke A's DoIt by invoking a method of the object C. See if the following code would satisfy you:
Regards,
- Lam -
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What Lam Thai's does is that the <code>doIt()</code>
is chaining of <code>super.doIt()</code>, recursively. But that is not the question has been asked.
Let's stick to the question in the current
context :
Originally posted by Micah Holroyd
For this question:
Given classes A, B and C, where B extends A and C extends B and where all classes implement the instance method, void doIt(). How can the doIt() method in A be called from an instance method in C?
Select the one right answer.
(a) doIt();
(b) super.doIt();
(c) super.super.doIt();
(d) this.super.doIt();
(e) A.this.doIt();
(f) ((A)this).doIt();
(g) It is not possible.
The question clearly asks that How can the doIt() method in A be called from an instance method in C?
The question does not mention that the <code>doIt()</code> has been implimented by a call to <code>super.doIt()</code> in each of the derived classes.
So the ANSWER IN THE CURRENT CONTEXT IS INDEED (G).
Hope this clears the issue.
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 10, 2001).]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
But I was under the impression that the question wanted a way to directly access A's method from an instance method in C.
The code below is an example of my idea:
I think Thomas' reply was the most correct out of all our answers, though. The question is worded a little ambiguously, so I have a point however picky it is. The only problem with my code, like Thomas said, is that the method in A will not have access to the current state of the C instance.
| It runs on an internal combustion engine. This ad does not: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









