| Author |
method call from super/sub object ref.
|
Richard Lakin
Greenhorn
Joined: Jan 22, 2002
Posts: 7
|
|
OK, given the following question..... ---------------------------------------- QUESTION: What code placed after the comment '//Place your code here.....' will result in calling the getChild() method resulting in the output of the string "child"? class Parent { } class Child extends Parent { public String getChild() { String name = "child"; return name; } public static void main(String argv[]) { Parent p = new Child(); //Place your code here..... } } ----------------------------------- Can someone please explain why the answer is :- -- System.out.println(((Child)p).getChild()); and not.... -- System.out.println(p.getChild()); I understood that method calls are resolved at Runtime using the class of the object (Child in this case) not the reference type (Parent). Or am I missing something basic?? Thanks in advance
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Have you tried compiling this (compile it with your answer and the given answer)? I think that, if you do, you'll find your answer in a big hurry. Corey [ April 18, 2002: Message edited by: Corey McGlone ]
|
SCJP Tipline, etc.
|
 |
Richard Lakin
Greenhorn
Joined: Jan 22, 2002
Posts: 7
|
|
Thanks, Corey, but I wasn't questioning the validity of the answer (I had already tried to compile it), just the reasoning. Compiling doesn't give me much other than to say that it 'cannot resolve symbol: method getChild()', which is not overly helpful. I was wondering if anyone out there could expand on this? If you can, please do!
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by Richard Lakin: Compiling doesn't give me much other than to say that it 'cannot resolve symbol: method getChild()'
That's exactly what I wanted you to see! The parent class doesn't have a getChild() method, but the type of the variable p is type Parent. You can't call the getChild() method on p because it doesn't have such a method. The compiler will issue an error in such a case (that's what you saw). You need to first cast the variable p as a Child (which has a getChild() method) so that you can call it. Since the Child class has a getChild() method, the compiler won't complain. Suppose the variable p referenced a Parent object, rather than a Child object. Could you then call getChild() on it? Of course not! The compiler doesn't know what the variable p will reference at run-time, so it has to validate that the method can be called on the type that p is at compile-time. I hope this helps, Corey
|
 |
Richard Lakin
Greenhorn
Joined: Jan 22, 2002
Posts: 7
|
|
.
Suppose the variable p referenced a Parent object, rather than a Child object. Could you then call getChild() on it? Of course not! The compiler doesn't know what the variable p will reference at run-time, so it has to validate that the method can be called on the type that p is at compile-time
OK, nearly there, but I think I'm getting slightly muddled with the late-binding/overridden issue where the method called is determined at runtime using the type of the object (here - Child) not the type of reference (Parent). This is why I thought it wouldn't complain about p.getChild(). So just to clarify then; where the method is not an overridden one, you have to explicitly state (ie, by casting) in which class the method you are calling has been declared? Is this correct? Your help much appreciated. Ta very much.
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by Richard Lakin: ...I'm getting slightly muddled with the late-binding/overridden issue...
There is no method overriding going on here! The Parent class doesn't have a getChild method, so it can't be overridden in the Child class. This is also the reason you can't invoke the getChild method on the Parent class - it doesn't have one! The late-binding issue is definitely a complicated one, but that IS NOT what is happening in this situation. Had this been the situation: Your line of code would have worked because, now, the Parent class has its own getChild method. In this case, we're involved in method overriding, polymorphism, and late binding. That's not the case in your original question. I hope that helps, Corey
|
 |
 |
|
|
subject: method call from super/sub object ref.
|
|
|