This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting" Watch "casting" New topic


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 ]

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();
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();

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

Joined: May 23, 2004
Posts: 5

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..
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166

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.

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
Thanks a lot!
I agree. Here's the link:
subject: casting
Similar Threads
Doubt Reg. Casting Object References
Type casting doubt
Object referernce casting
Casting between classes.
Object Reference Casting