File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting 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 "casting" Watch "casting" New topic
Author

casting

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
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 ]

Software_guy
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
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
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
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

Ab Sharma
Greenhorn

Joined: May 23, 2004
Posts: 5
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
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
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

Joined: Nov 07, 2003
Posts: 117
Thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting