The following code fails with ClassCastException on casting to Actor
Object[] roles = getAllRoles();
for(Object role : roles) {
if( role instanceof Actor) {
Actor server = (Actor) role;
The role class does not implement Actor interface but inherits its implementation from the super class
The problem disappears after adding clause 'implements Actor' to the concrete role class.
Complete sources to reproduce the problem are available on this link.
Explanation for the curious: the two statements internally
use different versions of role Actor:
The instanceof check uses Service.Actor, this check passes.
The cast is internally translated as a call to a dynamically bound
synthetic method which then attempts a cast to Shaving.Actor,
which wrongly fails (due to a missing "implements" link in the
generated byte code).
Of course, this difference concerning Service.Actor and
Shaving.Actor shouldn't surface here.