• 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

Polymorphism

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


Please help me to understand line #1.

Source:
Source of this problem

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

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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>((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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Keith uses more than two fingers to type!
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic