• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Casting Problem

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source:http://www.examlab.tk/ Practice Exam:3 Question# 48


In the above program, line 10 gives compile time error. And line no 9 throws CastcastException at run time. Both the errors/exceptions are related to casting. How do we decide whether the issue related to casting will be visible at compile time or run time. Does it have to do something with interface. Please explain.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to just copy/paste this reply by Henry again as it is essentially the same problem.

Originally posted by Henry Wong:
Here is a concrete example. Supposed that you have this...



then...



This is allowed -- even though C does not implement the interface I. The compiler only knows it as the class type B, and it is theortically possible for a new class, say D, to subclass B, and implement I.



This is *not* allowed. The compiler knows it as the class type C, and since class type C is a final class, it is not possible for a new class, say E, to subclass C, and implement I.

Henry



This answer was dealing with the final modifier but it gives a good explanation of what the compiler is looking at. In your example though A does not implement I so it throws a CastcastException at run time. At line 10 there is no way you could subclass D and also subclass A so the compiler complains because it knows at compile time that it's impossible. Hope that made any since.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At line 10 there is no way you could subclass D and also subclass A so the compiler complains because it knows at compile time that it's impossible.



Still have no clue why line 10 does not compile? The final class here is B not A.
[ November 21, 2008: Message edited by: Henry Zhi Lin ]
 
Sheriff
Posts: 7395
1413
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Zhi Lin:

Still have no clue why line 10 does not compile? The final class here is B not A.



Line-10 is...
D d=(A)c;
How can you cast from C to A, while there are NO any is-a relationship between A and C? That is the reason.
[ November 21, 2008: Message edited by: Devaka Cooray ]
 
Right! We're on it! Let's get to work tiny ad!
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic