I have a class which is extending another class and implementing one interface , so now what will be the reference type of the class.As now I getting ClasscastException
if I am trying to convert the base type into child type.
For example
public class Child extends Parent implements SomeInterface
{
}
SomeInterface childObject = new Child();
childObject2 = (Child)childObject;
or
Parent p = new Child();
childObject2 = (Child)p;
in both cases I am getting class cast exception
You can not cast the object of subclass to superclass.As superclass has generalized characteristics while subclass has special characteristics.
SuperClass sup = (SuperClass) new SubClass();
SubClass sub1 = (SubClass) sup;
You can refere : Casting super class to sub class
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.