File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downcast code question

 
joe bob
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guy,

Why the downcast in this code worked, i know if super class casted down to sub class it generates runtime error, and why it prints 'c' instead prints 'b'?? Thanks guys



 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're actually creating a C object on line 3.

C extends B, so this C is also a B. Therefore when you try and cast the variable to a B, it works. You get an exception when you cast a super-class object to a subclass, but you aren't actually doing that here. The real type of the object is C, which is fine.

Calling the method therefore calls the version in C, as you'd expect - this is polymorphism.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic