aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Problem" Watch "Casting Problem" New topic
Author

Casting Problem

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
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.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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.


SCJA
~Currently preparing for SCJP6
Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69
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 ]

SCJP 5.0
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

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 ]

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Problem
 
Similar Threads
thread
Regarding Casting between classes and interface
Reference Type Casting
book K&B self test q16........problem
Threads and Synchronization examples