• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from JavaChamp.com Mock Exam

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ques:

Answer : The code compiles fine but throws a RuntimeException because of the second casting

Vehicle is Colorable, both exist in same inheritence tree. so base-class can hold its sub-class object. then why this code gives exception for second casting. Please give explaination. And if possible also provide theory/link for this concept so i can know more in depth of this concept.
promila
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because at runtime jvm finds out that the actual object is of type Vehicle which doesn't implement colorable.

this topic has been discussed a lot of times on ranch just give it a search.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vehicle isn't colorable, Car is colorable. Vehicle doesn't implement Colorable, so it can't be cast to it, whereas Car does implement Colorable, so it can be cast as such.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
promila please QuoteYourSources for the question. Questions without proper source are entitled to be deleted...
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:promila please QuoteYourSources for the question. Questions without proper source are entitled to be deleted...

\

Isn't the title JavaChamp.com Mock exam a proper source? I've never been to the site so I'm not aware of what kind of mocks it has.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what's wrong with me, yes the source is mentioned alright
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Promila,


We added recently a new feature to post inquiries or comments to each question (look for the happy face below each question .

This will enable each user to follow the discussion thread related to this question.

http://www.javachamp.com/questions/casting-objects-in-java.xhtml

Best Regards,

JavaChamp Team
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Promila,


class Car knows about (extends) class Vehicle and it also knows about interface Colorable(implements).

So instance of Car can be typecast to any of its upper level class or interface as done in first case.

But class Vehicle has no idea about interface Colorable. Hence its instance cant be typecast to Colorable.

In terms of JVM, it asks what is Colorable.???

Please correct me if i am wrong...

 
Yasser Ibrahim
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But class Vehicle has no idea about interface Colorable




Hi Shailesh

Your analysis is absolutely correct.

I attached a UML class diagram that depicts the class inheritance for clarity.

Best Wishes


JavaChamp Team
QuestionUML2.jpg
[Thumbnail for QuestionUML2.jpg]
UML class diagram that depicts the class inheritance
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic