[Logo]
Forums Register Login
"super" question (Mughal 6.6)
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 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.
The question could be worded better. The question should state that you want to run A's doit() "on" this instance of C, not "from" this instance of C. The problem is, of course, that if you run A.doit() the way you want to it will not have access to the current state of the C instance.
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>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.

 
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).]
 
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 -
Hi Folks,
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).]
Thanks for your code sample, Lam. It does access the correct method by seperating the two supers into different classes. It steps up the hierarchy, rather than trying to put the two supers in one method call.
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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Oct 19, 2017 14:21:32.