• 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

casting

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes casting up the inheritance tree gives runtime error.Can anybody explain actually when it will give runtime erro during casting and when will it not?
subclass=(subclass)superclass- will it give runtime error?
[ May 25, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A runtime error is caused (ClassCastException specifically) when you try to cast something to a type that it is not.

For example, look at this code:



In this example, our reference variable, a, references an object of type Cat. Therefore, line 1 will execute fine as we're casting something of type Cat to type Cat - that is perfectly legal.

However, line 2 will cause an error because a Cat is not a Dog. The cast is illegal and will cause a ClassCastException at run time.

It's not that complicated, really - you get a ClassCastException when you try to cast an object to a class that it can't be cast to.
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the detailed explaination.How about the following?
Animal a = new Animal();
Cat c=new Cat();
c=(Cat)a;
Will the above passes the runtime?Can an animal be casted in to a cat?Please let me know.Thanks
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Animal a = new Animal();
Cat c=new Cat();
c=(Cat)a;



No the above will give u a ClassCastException.
You r trying to cast a superclass object to a subclass object which cannot be done. Although opposite of this will work. The following code will work

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

You must have tried and know the answer for your query.
The explanation is :-

ClassCastException occurs whenever the actual object does not have a "is a" relationship with the class it is being casted to.

Lets analyze this rule in context with the ablve example:-
Cat "is a" animal.
So if a cat object is casted to an animal its well and good.

Example cited in the second post first casts the cat object to an animal. So when this object is being casted to a Dog, obviously exception occurs as Cat "Is NOT a" Dog.

Creating an Animal object and casting to a Cat is again not aceptable as Animal "is NOT a" cat .. only reverse is true.

Hope that was useful..
Regards
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sridhar,

For the code:

Since A is not a Cat so it cannot be casted into a Cat.

For the code:

You can cast A into Cat because A is a Cat and upcast into an Animal type. So it can be downcast into a Cat finally.

Thanks,
Jack
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic