/**** * * In FMSL terms, Integer < Object, i.e., obj Object is Integer or ... ;. And * we have that an Integer "can be treated as" an Object, but not the other way * around (see comments to this effect in the code below). "Can be treated X" * means formally "can be bound to an identifier of type X". * * Now, given this general idea, we can deal with implicit projection/injection * of types out of/in to union types. Specifically, a value of a type that is * one of the element types of a union can be bound to an identifier of the * union type, but not the other way around. I.e., ElemType < "union of * ElemType, ... " means we can bind a value of ElemType to an identifier of * Type "union of ElemType, ... ", but not the other way around. E.g., * see fmsl/testing/implementation/new-inputs/another-union-test.rsl. * */ public class PolymorphismCheck { public static void TakesObject(Object o) {} public static void TakesInteger(Integer i) {} public static void main(String[] args) { /* * Next line is legal, since Integer can be treated as Object. */ TakesObject(new Integer(1)); /* * Next line is illegal, since Object cannot be treated as Integer. * Compile time error is "TakesInteger(java.lang.Integer) cannot be * applied to (java.lang.Object)". */ TakesInteger(new Object()); /* * Next line is Legal at compile time, but throws a ClassCastException * at runtime, since we're casting a non-Integer value to Integer. * I.e., there's no way that a new Object can be an Integer. */ TakesInteger((Integer)(new Object())); } }