Hatem, you just need to understand that you cannot type cast between a single element type and an array type, like
This is applicable for both primitive and objects. The only exception is Object type. Since it is the super type of all the types, you can assign an array to it. So any cast between a reference type will not work.
Now a cast between two non-related classes is not allowed. So if you try
Here
String and Integer classes are in different hierarchy so the cast is not allowed. But a cast is allowed between an interface type and another interface type or class.
The basic idea behind this is that at runtime, the cast might be successful as there might be a type, which is sub-type of both the interfaces or one interface or class. The cast between C1 and C2 is not allowed because under no circumstances, can there be an object which is compatible with both (as a class cannot extend multiple classes). Suppose I create a class like this
Now if we review the original type casts, then they will all work
But the cast is not allowed between a non-related class and interface, if the class is final
In this case there cannot be a class, which is a sub-class of both I1 and C1 (as C1 is final). This is why the cast will generate a compilation error.
HTH...