import java.awt.Rectangle;

public class AP
{ /* It is OK to assign an object to a variable of
an interface type. The object has to come from a class
that implements the interface.
It is also OK to 'recover' an object from a variable
of an interface type.
*/

public static void tryThis()
{ // The following is OK. We're assigning a String
// to a Comparable interface type. The String class
// implements the Comparable interface.
Comparable a = "Hello";
System.out.println("a: " + a);

// The following is not OK. The Rectangle class
// does not implement the Comparable interface.
//Comparable b = new Rectangle(5, 10, 15, 20);

// The following is OK. We can use a cast to
// 'recover' a concrete object from an interface
// type variable.
String c = (String) a;
System.out.println("c: " + c);
}

/* illustrate polymorphism
An interface variable and a method call can
give different results,
depending on which class was last assigned to it.
Here, x.compareTo() prints -3 the first time
and -1 the second time, because different underlying
classes are involved. */
public static void tryThis2()
{ Comparable a = "Tuesday";
Comparable b = "Wednesday";
Comparable byte1 = 7;
Comparable byte2 = 11;
Comparable x = a;
Comparable y = b;
System.out.println(x.compareTo(y));
x = byte1;
y = byte2;
System.out.println(x.compareTo(y));
}

public static void main(String[] args)
{ AP.tryThis();
AP.tryThis2();
}
}