File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting and Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting and Polymorphism" Watch "Casting and Polymorphism" New topic

Casting and Polymorphism

David Swan
Ranch Hand

Joined: Oct 08, 2003
Posts: 33
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???

Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
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
David Swan
Ranch Hand

Joined: Oct 08, 2003
Posts: 33
Ahh hah, i get it now. Thats makes sense now, and has answered some questions that i didnt even know i had...!!!
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Casting and Polymorphism
It's not a secret anymore!