I have a Person class and an empty class called MyClass and an empty interface called MyInterface. There is no relationship between the Person, MyClass and MyInterface.
When I cast a Person instance to MyInterface, the compilation passes, but when I cast a Person instance to MyClass, the compilation fails. Why is that?
Below is my code:
class Person {
public String name;
}
class MyClass {}
interface MyInterface {}
class Test {
public static void main(String[] args) {
// Compile passed
MyInterface myInterface = (MyInterface) new Person();
// Compile error
MyClass myClass = (MyClass) new Person();
}
}