Casting a reference will only work if it's an instanceof
that type. You can't cast random references. Also, you need to read more on Casting Objects
.
e.g.
String string = "String";
Object object = string; // Perfectly fine since String is an Object
String newString = (String)object; // This only works because the `reference` object is pointing to a valid String object.