• 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

Class Casting Question?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Base reference does not point to the appropriate subclass object, hence a ClassCastException is raised.

Is this what you wanted to know?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James,

I understood. Good and simple explanation.

Regards,
Jothi Shankar Kumar. S
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic