wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""super" question (Mughal 6.6)" Watch ""super" question (Mughal 6.6)" New topic
Author

"super" question (Mughal 6.6)

Micah Holroyd
Greenhorn

Joined: Jan 22, 2001
Posts: 20
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.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
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.

Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
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

Joined: Apr 02, 2001
Posts: 117
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

Joined: Mar 16, 2001
Posts: 216
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

Joined: Jan 22, 2001
Posts: 20
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "super" question (Mughal 6.6)
 
Similar Threads
Calling a Overriding method in a abstract class
neeed help !!!!
Use of THIS and SUPER : Clarification Needed
Access
what does "method" mean?????