• 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

Casting and Polymorphism

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I am just wondering about this code that i have, i dont think i know how polymorhism quite works, heres my code
class Base {
protected int size = 100;
public int getSize() {
return size;
}
}
class Runtime extends Base {
protected int size = 10;
public int getSize(){
return size;
}
public static void main(String[] args) {
Runtime r = new Runtime();
System.out.println (r.size + "," + ((Base)r).getSize()); <-- problem
}
}
i dont understand why i get the result of 10,10 printed to the screen, i would have thought it was 10,100 because i cast my r object to Base.
Can anyone share in my frustration???
Cheers
Dave
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dave,
casting an object does not mean to change the class-type of the object. It only says to the compiler: "Even if you cannot see that this object I want to use is an instance of class C, I assure you, at runtime, it will be." That's all.
So one of the most common uses of casting is working with the objects of a collection, for example of an ArrayList. If *you* know that in the ArrayList there are only, let's say String objects, you tell the compiler by casting that the following operation is allowed:

Without the cast, the trim method would not be allowed, because from the sight of the compiler, in an ArrayList there are only "Objects".
So in your example the cast is only to irritate one. r is an instance of Runtime, and because Runtime extends class Base, r is an instance of Base (in the sense of instanceof), too. But: The method wich is called depends on the runtime type (this is why the class is called runtime, knick knack), so: on the type r.getClass() would return. And that is, no matter if you use the cast or not - Runtime.
So it's 10,10.
Hope it helps
Detlev
 
David Swan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Detlev
Ahh hah, i get it now. Thats makes sense now, and has answered some questions that i didnt even know i had...!!!
Cheers
Dave
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just my 2cents to what Detlev has explained. Member variables are class bound, overridden methods are runtime type bound. Polymorphism doesn't apply to static and inherited methods - their invocation is all determined by the reference class type.

The output is:
Doing Runtime stuff 100
Doing Base stuff 50
Stuff is now 50
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A real-world analogy of this:
a Duck-Billed Platypus is a Monotreme (an egg-laying mammal). So if you were to represent it in a class structure, it would be:

Now, the Animal Class defines an (abstract) "giveBirth()" method. TheMammal class overrides this and calls "giveBirthToLiveYoung()" as then entire body of the method. The Monotreme class overrides "giveBirth()" and
calls "layEggs()" as the entire body of the method.
Now, you instantiate a Platypus and cast it to a Mammal. However, when you invoke the "giveBirth()" method, it will still "layEggs()." Simply calling a Platypus a Mammal does not make it give birth to live young -- no matter what, it will lay eggs.
reply
    Bookmark Topic Watch Topic
  • New Topic