• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

it's polymorphism or a upcasting

 
Alexandra Zhang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learn your dog polymorphish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic