You can't cast them if they are not realted .
The class of the object you’re casting and the class you’re casting it to must be related by inheritance; that is, you can cast an object only to an instance of its class’s sub- or superclass-not to any random class.
Side note
: Casting downward in the class hierarchy is automatic, but casting upward is not.
11
solved java arraylist iteration having a ClassCastException