• 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

Dynamic Polymorphism: new method in subclass

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to create an dynamic object at runtime. In creating an object of the superclass reference and then reassigning the value of the subclass reference I receive this error during compile:
app.java:37: cannot resolve symbol
symbol : method doThis ()
location: class a
aref.doThis();
^
1 error
What is wrong with this code?

(edited by Cindy to format code)
[ January 24, 2002: Message edited by: Cindy Glass ]
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the nature and basic concept of casting and polymorphism.
basically because aref is a refrence to class A it always will check first in class a for the method and variables. (even if it actually points to object of class B).
if it finds a method there like print() then IF there is an overriden version of print in class B it will perform the print of class B (if there wasnt one he would have performed the one in class A).
however, if u define new function OR variables in the class that inherit from A (like doThis() for example) then the compiler gets stuck.remember that the compiler thinks aref refers to class Aso he doesnt find any doThis() there.
in order to tell the compiler that he actually points to class B in this case we need to do casting like this:
((B)aref).doThis();
now we tell the compiler to look at aref as a refrence to class B in this sentence so he will perform the action!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the point when you call doThis( ), aref is a reference to a "b" object. Since b defines the method doThis( ), you should be able to call it - and you can, but not simply by the call aref.doThis( ) - the compiler doesn't know that aref is a "b" object - all it knows is that aref is a reference to an "a" object - so it complains that "a" doesn't have a method doThis( ).
You could cast it like this:
((b) aref).doThis
Tony
 
Darren Johnston
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic