Java rules for casting

When can a certain object be cast into another object? Does the casted object have to be a subtype of the other object? I’m trying to figure out the rules…

Edit: I realized that I didn’t explain my issue at all: basically I am casting an object to an interface type. However, at run-time, I get a java.lang.ClassCastException. What needs to happen with my object so that I can cast it to this interface? Does it have to implement it?

Thanks

Leave a Comment