aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class Casting Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class Casting Question?" Watch "Class Casting Question?" New topic
Author

Class Casting Question?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Hi Guys,

I have a question regarding class casting. From a peripheral viewpoint, casting down the inheritance tree would result in a run time error. consider the below code,



Regards,
Jothi Shankar Kumar. S
[ October 16, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
The Base reference does not point to the appropriate subclass object, hence a ClassCastException is raised.

Is this what you wanted to know?


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Hi Above,

My question was if I can cast a superclass reference to a subclass reference? And also if I do this, I just get a run time wrror rather than compile time? Can you elaborate on this?

Regards,
Jothi Shankar Kumar. S
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

Jothi Shankar Kumar Sankararaj asked
My question was if I can cast a superclass reference to a subclass reference?


Yes, if the reference type is superclass but the object type is subclass:




Yours,
Bu.
sundaramoorthi thangavel
Greenhorn

Joined: Jul 18, 2006
Posts: 6
We must remember that those variables are really object references. They are pointing to appropriate objects in the memory. Memory is allocated at runtime. A parent class reference variable can point to child class objects also. At the time of compilation, it is difficult to say the type of object the parent class reference variable is referring. So the compiler won't say anything at the time of compilation. Real type checking is carried out at run time only. If incorrect casting is carried out, a runtime exception is thrown. I think it clears your doubt.
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Above,

My question was if I can cast a superclass reference to a subclass reference? And also if I do this, I just get a run time wrror rather than compile time? Can you elaborate on this?

Regards,
Jothi Shankar Kumar. S

only when two unrelated classes casting will give you compile-time error. say:
class A{}
class B{}
A a = (A)new b();// this line gives you compile-time error.

if any inheritence hiarachy exists, you won't see error until run-time.

however, interface is the exception. Given:
Interface A{}
class B{}
A a = (A)new B();// this line doesn't give you compile-time error even though B is not implementing A
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Thanks James,

I understood. Good and simple explanation.

Regards,
Jothi Shankar Kumar. S
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
James gave this example:
however, interface is the exception. Given:
Interface A{}
class B{}
A a = (A)new B();// this line doesn't give you compile-time error even though B is not implementing A


But this will give you a warm and friendly
ClassCastException



Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Class Casting Question?