• 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

cast problem

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


Is there a way that I can use Animal's checkup method on a Cat object?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can define another method inside Cat, for example animalCheckup(), and then call super.checkup() inside it.

But what you are trying to do in your code won't work due to the fact that polymorphic resolution of instance methods is based on the type of the object. You need to modify the Cat class for this to work (not considering the fact that there should be ways to make this work with introspection.)
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A friend of mine told me that C++'s dynamic cast can do this by changing the pointers in the v table , is that true? Is there similar things in Java? I used to think the cast in Java is dynamic cast. Thanks~
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Men, you are going to have to be more specific about what you mean by dynamic cast.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Men, you are going to have to be more specific about what you mean by dynamic cast.


(((Animal)someAnimal).checkup()) in C++(might be different syntax), will acturally use animal's checkup method instead of cat's?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

C++ provides the option of having a method support (or don't support) polymorphism. Java doesn't allow this -- all methods are "virtual" (C++ term).

Men Gumani wrote:A friend of mine told me that C++'s dynamic cast can do this by changing the pointers in the v table , is that true?



Yes, this is true... but it is silly to do. Basically, your friend is saying to modify the jump tables, to trick the system into thinking which is the latest version of a method. Java doesn't support pointers, so this is not possible.

Henry
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic