Question

Can anyone here please explain to me why I get a java.lang.ClassCastException when downcasting a Parent to a Child?

public class Child extends Parent{
public static void main(String[] args){
    new Child().go();
}
void go(){
    go2(new Parent(), new Child());
    go2((Child) new Parent(), new Child());
}
void go2(Parent p1, Child c1){
    Child c2 = (Child)p1;
    Parent p2 = (Parent)c1;
} 
}
class Tree{}

I have read reference variable casting and searched for examples in the web. Can someone please explain it to me? I really want to understand why it throw exception. Thanks

Was it helpful?

Solution 4

because of polymorphic reference, when you store a sub-type object to super-type reference, you can't use method specific to sub-type. but you can just get which are methods defined in super-type.

to be able to use sub-type methods, you need to cast the reference of super-type to sub-type.

Parent p = new Child();
...
...
((Child)p).childMethod();

but this is only possible when you know object assigned to p is actually of type Child. else it will give ClassCastException

OTHER TIPS

Lets say you Parent class Animal, and Child class Bird. You can cast from Bird to Animal, because all Birds are Animals. You CANNOT cast from Animal to Bird, because not all Animals are Birds.

Here is your problematic Code stripped:

go2(new Parent());

...

void go2(Parent p1){
    Child c2 = (Child)p1;
}

The compiler allows your cast of p1 from 'Parent' to 'Child' because p1 could be a 'Child'. You can however obviously see, that p1 is NOT a child. So your code is fine at compile time, but will crash at runtime.

That means, all added behaviour which you may have defined in your 'Child' class is not available since you only have an instance of Type 'Parent'.

You can cast a Child to a Parent. Since Child extends Parent, any instance of a Child is a Parent (but with extra stuff only available to a Child). However, p1 is a Parent. Since a Parent is not a Child, you can't cast an instance of a Parent to a Child.

I copied exactly the answer to a similar question form a google search. Please do your homework next time and read the FAQ.

A Parent is not a Child. That is, a Parent does have the necessary fields to be treated as though it were an instance of Child and fulfill all the Child specific methods - the contract of Child. Imagine if Child had a variable childOnly and a method childProcess and you tried to call childProcess on an object that is a Parent in memory. It could not possibly work, so Java prevents you from treating Parents as Childs.

Lets say Car is a bare-bone interface

Merc is a Car (Merc extends Car)

BMW  is a Car (BMW extends Car)

now

you can take BMW and treat it bare-bone car as BMW as all the features of a Car. so you can Cast BMW to a Car so (Car car = (Car)bmw) is legal.

If you take a bare-bone car and try to treat it as BMW, it cannot be done as BMW will have a lot more than a bare-bone car.

so,

BMW bmw = (BMW)car is illegal

The same real world concept is extended to Object Oriented world.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top