// Casting of class
ArrayList a1 = new ArrayList<Integer>();
Number n1 = (Number)a1; // clause 1 Error
AdjustmentListener c1 = (AdjustmentListener)a1; // Clause 2 No error
EventListener c3 = (EventListener)a1; // Clause 3 No error
Based on the above, clause 1 has error. Logical. There is no relation between those 2 classes. For clause 2, there is no relation between an arrayList (a class) and the adjustmentlistener interface. Why is there no error?
EDIT 1:
String str1 = new String();
AdjustmentListener c2 = (AdjustmentListener)str1; // clause 4 compiler error
In order for a cast to succeed, there must be some possible implementer of both arguments. Take, for example, the following imaginary class declarations:
An instance of each would allow the cast to succeed. For clause 4, in order for the cast to work, you would need
and Java does not allow multiple inheritance.
EDIT for edited question:
In clause 1, there is no possible relationship between the classes, because you cannot have a class extend both ArrayList and Number. In clauses 2 and 3, it is possible to create a relationship by creating a new class that extends ArrayList and implements AdjustmentListener. Then set a1 to an instance of that class.