A reference to an object can be cast into a reference to an object of its implemented super interface : Reference Type Casting « Type Casting « SCJP






public class MainClass{
    public static void main(String[] argv){
       MyInterface i = new MyClass();
    }
}
    
interface MyInterface{
    public void aMethod();

}
interface MySubinterface extends MyInterface{
    public void bMethod();
}
class MyClass extends MySubinterface{

    public void aMethod(){
    }
    public void bMethod(){
    }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from MyClass to MyInterface

	at MainClass.main(MainClass.java:3)








4.3.Reference Type Casting
4.3.1.Object reference conversion
4.3.2.An interface type can be converted to an interface type or to Object.
4.3.3.Convert interface to super-interface
4.3.4.Object Method-Call Conversion
4.3.5.Cast between an interface and a nonfinal object.
4.3.6.Runtime casting: the class being converted must be itself or must inherit from it.
4.3.7.If New type is an interface, the class of the expression being converted must implement New type.
4.3.8.For = with object references, if the type of the left operand is a class C, then the type of the right operand must be a subclass of C or the null value.
4.3.9.If the type of the left operand is an interface I, the type of the right operand must be a subinterface of I, or a class that implements I, or the null value.
4.3.10.A reference to any object can be cast into a reference to an object of class Object.
4.3.11.A reference to an object can be cast into a reference to an object of its parent class.
4.3.12.A reference to an object can be cast into a reference to an object of its implemented interface
4.3.13.A reference to an object can be cast into a reference to an object of its implemented super interface
4.3.14.Casting Object References with Collections
4.3.15.An Object reference can be converted to:
4.3.16.Use a reference variable to refer to any object that is a subclass of the declared reference variable type