When you press o1 and o3 on (I2) you tell the compiler that the class of the object is actually a subclass of its declared type and that this subclass implements I2 .
The Integer class is final , so o3 cannot be an instance of the Integer subclass: the compiler knows that you are lying. C1 however, is not final, so o1 can be an instance of the subtype C1 , which implements I2 .
If you do C1 final, the compiler will complain too:
interface I1 { } interface I2 { } final class C1 implements I1 { } class C2 implements I2 { } public class Test{ public static void main(){ C1 o1 = new C1(); C2 o2 = new C2(); Integer o3 = new Integer(4); I2 y = (I2)o3;
WilQu Apr 24 '13 at 8:01 2013-04-24 08:01
source share