Trying to understand java narrowing conversion from a class to an interface. The JLS(JLS-5.1.6) states:
From any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.
To test this, I created a class and an interface. Then tried casting the class to the interface, but get a run-time ClassCastException. This is a sample of my code.
class NarrowingReferenceConversion
{
public static void main(String args[])
{
S s = new S();
T t = (T)s;
}
}
interface T
{
public void print();
}
class S
{
public void print(){
System.out.println("S.print()");
}
}
On compiling and running the above I get the following error message:
Exception in thread "main" java.lang.ClassCastException: S cannot be cast to T