Casting class to interface in java -
this question has answer here:
why interface allow classes?
for example have below code :
interface i1 { } class c1 { } public class test{ public static void main(string args[]){ c1 o1 = new c1(); i1 x = (i1)o1; //compiler compile failed in run time } }
i know why failed in runtime because c1 class not implements i1 interface. if class c1 implements i1 above code work successfully.
can explain why interface allow class casting?
there a
class c2 extends c1 implements i1 { ... }
an object of class assigned o1
, cast ok @ runtime.
Comments
Post a Comment