wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green#3: Q57 - Underlying Object Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green#3: Q57 - Underlying Object Type" Watch "Marcus Green#3: Q57 - Underlying Object Type" New topic
Author

Marcus Green#3: Q57 - Underlying Object Type

Brett Swift
Ranch Hand

Joined: May 22, 2002
Posts: 61

The answer to this question is 4).
However: I thought that because of the line:
Base a = new Agg();
that a was of type Agg. And I have seen in other examples, that when the instance goes for a method call, it will call the method in the class of the underlying object type.... hence the Agg method in this case.. Obviously I'm WRONG!
What am I missing on this concept?
Thanks!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Brett Swift:
However: I thought that because of the line:
Base a = new Agg();
that a was of type Agg. And I have seen in other examples, that when the instance goes for a method call, it will call the method in the class of the underlying object type.... hence the Agg method in this case..[/QB]

That's true...when you have an overridden method. There aren't any overridden methods here! The reason the cast is required is becuase the class Base doesn't have a getFields() method. Therefore, you have to cast it to a Agg class in order for the compiler to allow you to make that call on a variable of type Base.
I hope that helps,
Corey


SCJP Tipline, etc.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi all! Just want to ask a few questions about this question.
From Marcus's answer:
The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which displays Base. But the call to ((Agg)a).getField() will invoke the getField() in the Agg class.

I'm confused at this part:
So, a.getField() invokes getField() in the Base class, which displays Base.
But the class Base doesn't have a getField() method, in fact, it's empty!
Another thing: in general, you can't call any methods in the subclass not declared in the parent class if you use a reference of type parent class to refer to the subclass, right?
[ June 04, 2002: Message edited by: Paul Villangca ]
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479

Another thing: in general, you can't call any methods in the subclass not declared in the parent class if you use a reference of type parent class to refer to the subclass, right?
[ June 04, 2002: Message edited by: Paul Villangca ][/QB]

That's true, unless you cast the reference! That's exactly what Marcus does, so you are using a reference of type subclass to refer to the subclass! Everything goes well


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Marcus Green#3: Q57 - Underlying Object Type
 
Similar Threads
Runtime
overriding methods, please help
Marcus Green Exam 3, Question 57
Marcus Green #Exam 3 Q57
Marcus Green Exam 3 Q57