简体   繁体   中英

Class Casting Exception in Java

I'm having a problem with casting Classes to each other.

To explain it in more detail, take a look at this image.

图片

In my code, I do SkillBase s = new SkillBase(); and then set some values in that class. Then I try to do Subclass sub = (Subclass)s; but when running it it gives a ClassCastException.

I added a small debug part, which checks if it's instanceof which returns true.

I've tried the google, saw some questions on here aswell and read them (none of them had an answer that was for me)

So, what do I do?

SkillBase is not an instance of Subclass , so why do you think casting will work? Try with this:

SkillBase s = new Subclass();
Subclass sub = (Subclass)s;

which will succeed. Also I think you are not correct with instanceof , I am certain that:

s instanceof Subclass

yields true in the the code above but false in your case.

Thinking in real world terms: you can always cast Dog to Animal because every dog is an animal * , but casting Animal to Dog might fail since some animals aren't dogs.

* in fact, compiler does that for you, it is known as polymorphism

You can only up-cast ie assign sub classes to super class references. Think of it this way: Subclass extends the Superclass by adding a new method f() . Now Superclass doesn't know any thing about f() and hence the problem.

You're probably using instanceof wrong.

The runtime is right to crash, as SkillBase is not a Subclass .

The other way around is true.

s can't be cast to Subclass because it wasn't instantiated as a Subclass. It is a Skillbase. If you want to use a Subclass why not just instantiate it?

Subclass s = new Subclass();

You can still set the parameters you want because they hare inherited by Skillbase and the cast is no longer necessary.

The problem is that you are not using casting properly. A class can always be cast as its parent, but not the other way around. The concept is that a child class (by definition) knows the structure of the parent, and (by definition) already supports all the signatures in the parent. Consequently, the parent is a subset of the child.

The inverse, however, is not true. The parent class knows nothing about the child class and/or whether the child has added extra signatures to its structure. Consequently, there is no way to tell the compiler to treat the parent as a child and make available all the methods that the child has in the parent.

It layman speak, water (child) is a liquid (parent), but not all liquids are water. Consequently, any measurements you want to make on liquids (ie: quantity, viscosity, etc) hold true for water as well, but not the other way around (ex: density of water is completely different than density of oil).

So to bring this all back to your situation, you can cast Subclass as a Skillbase but not the other way around.

Subclass instanceof Skillbase == true
(Skillbase) new Subclass() - also valid
(SKillbase) new Sub#2 - also valid

etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM