C
Chad
Given the following...
class X {}
class Y extends X {}
class Z extends X {}
public class Main {
public static void main(String[] args) {
X x = new X();
Y y = new Y();
Z z = new Z();
Y o6 = (Y) x; //<--error
}
}
I get this error...
run:
Exception in thread "main" java.lang.ClassCastException: X cannot be
cast to Y
at Main.main(Main.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I thought I could cast X to Y since they inherited. Why doesn't it
work in this case?
Chad
class X {}
class Y extends X {}
class Z extends X {}
public class Main {
public static void main(String[] args) {
X x = new X();
Y y = new Y();
Z z = new Z();
Y o6 = (Y) x; //<--error
}
}
I get this error...
run:
Exception in thread "main" java.lang.ClassCastException: X cannot be
cast to Y
at Main.main(Main.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I thought I could cast X to Y since they inherited. Why doesn't it
work in this case?
Chad