In the following example, why does the compiler not report an error for casting x to D?
interface X {}
class C implements X {}
class D {}
public class CastIssue {
public static void main(String[] args) {
X x = new C();
// why is there no error here?
D d = (D) x;
}
}
But, if we make X a class, the compiler reports an error:
class X {}
class C extends X {}
class D {}
public class CastIssue {
public static void main(String[] args) {
X x = new C();
// this gives a compile-time error
D d = (D) x;
}
}