Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting objects (OCAJP7, Mala Gupta)

 
Priyavrat Kumar
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, even though there is no is-a relationship between blackink and Printable, the code compiles successfully.
But removing the explicit cast makes code fail to compile.
Why?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyavrat Kumar wrote:Why?

I recommend having a look at this topic and this one (about exactly the same code snippet), this thread (also similar code snippet and an additional one) and this thread (about casting or using instanceof operator on a class vs an interface).

Hope it helps!
Kind regards,
Roel

PS. On CodeRanch, when you copy questions from a book, mock exam or other source, you are required to QuoteYourSources. This time I updated the subject and added the source for you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic