This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes it's polymorphism or a upcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "it Watch "it New topic

it's polymorphism or a upcasting

Alexandra Zhang

Joined: Jun 08, 2007
Posts: 16
Here is the code:

In line "Animal a1 = d;" my understanding is it's a polymorphism, because d is a newly declared dog, and a1 is assigned to be it. But in the guide book, it says this line is an implicit upcasting. What's wrong with my uderstanding?

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Casting and polymorphism are 2 completely different things. That line uses implicit upcasting and polymorphism.
If your unsure what polymorphism is you could read this.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The polymorphism comes in force upon invoking a method on Animal when it's dynamic type is Dog. If the method is overridden in Dog, the Dog's version gets executed, even though the static type is Animal. And this is called Polymorphism.

JDBCSupport - An easy to use, light-weight JDBC framework -
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Learn your dog polymorphish
I agree. Here's the link:
subject: it's polymorphism or a upcasting
It's not a secret anymore!