• 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" question (Mughal 6.6)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 -
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Micah Holroyd
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic