*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism 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 "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Please help me to understand line #1.

Source:
Source of this problem

Thanks,


cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Chandra Bhatt:


Please help me to understand line #1.

Source:
Source of this problem

Thanks,


Because the method echoN is defined in DerivedQ48 and not in TestQ48, when you use a TestQ48 reference to refer to a DerivedQ48 object, you cannot call the echoN method.

So first you must cast the reference type to a DerivedQ48. Then you can call the echoN method.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
>>((DerivedQ48)tq).echoN( tq.paramA );

The reference tq is of type TestQ48. In which case the compiler will look for that method echoN() in the base class, if the typecast hadn't been there.

This line is to tell the compiler, "don't complain, I know what I am doing and just use the one in the derived class". In other words, forcing the casted class' method to run.

Even if the same method is present in the base class, only the casted class' method will run.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The first thing to do is to format it properly:


Now look at the two lines:


OK: tq is a TestQ48 which does not have an echoN method. But we know that tq really refers to an object of type DerivedQ48 which does have an echoN method. The downcast is OK an compiletime and runtime.

As for the parameter: the object has two paramA members: one in the superclass TestQ48 part and another in the DerivedQ48 part.

tq.paramA is the value (9) of the paramA in the superclass part.
And that's what gets printed by the echoN method.
[ May 01, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
It prints answer as 9, that means in line 1 paramA is being passed as 9 from the main().

How come it is 9 and not 3 eventhough tq is pointing to DerivedQ48 ?
or am I missing something here.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I think Keith uses more than two fingers to type!
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108

M Krishnan wrote
How come it is 9 and not 3 eventhough tq is pointing to DerivedQ48 ?


variable refered depends on Reference Type (prints 9)
methods that can be called depends on Reference Type (To call echoN cast was necessary)
method called at runtime depends on the Object Type.
[ May 01, 2007: Message edited by: swarna dasa ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Keith and Barry for time prompt and fantastic reply.


BTW Krishnan, Member variables don't come under polymorphic call.


Thanks,
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
>>BTW Krishnan, Member variables don't come under polymorphic call.
Yes,yes. I wasn't awake while typing that and "tq." before paramA tricked me.
 
wood burning stoves
 
subject: Polymorphism
 
Similar Threads
need help..
print till the next blank line
please explain this
Variables
which one is recommended ?