I have an awkward problem with this (propably) simple casting example. Could you please help me?
public class Example1 {
interface ParentIf{}
interface ChildIf extends ParentIf {}
interface OtherIf {}
class ParentCl {}
class ChildCl extends ParentCl {}
class OtherCl {}
public static void main(String[] args) {
ChildIf cI = null;
ParentIf pI = null;
OtherIf oI = null;
ChildCl cC = null;
ParentCl pC = null;
OtherCl oC = null;
cI = (ChildIf)oI; //case1 - fine
cC = (ChildCl)oC; //case2 - inconvertible types
cI = (ChildIf)oC; //case3 - fine
}
}
But more awkward is that I don't know why the other two statements are fine.
I can't see any connection between OtherIf and ChildIf. So how is possible to cast the OtherIf to the ChildIf when there is no "extend" between these two interfaces in case1?