public class Role<A extends Actor> {
protected A actor;
protected A constructActor() {
return new Actor();
}
}
Why won't this Java code compile? My IDE tells me that constructActor() needs to return an A type, but not an Actor type. But as far as I understand I made it clear that A will be an Actor or an extension of an Actor. So why does Java behave this way?
Edit:
You might help me further with my idea of a "generified" factory pattern:
The constructActor() method actually wasn't intended to always return an instance of Actor (therefore the protected access), but to be some kind of default case for every child class that doesn't has to construct a specified version of the Actor:
// the first two cases should construct specified versions of the Actor
public class HeroRole<HeroActor> {
@Override protected HeroActor constructActor() {
return new HeroActor();
}
}
public class EnemyRole<EnemyActor> {
@Override protected EnemyActor constructActor() {
return new EnemyActor();
}
}
// the following classes should construct a regular Actor by default:
public class AnimalRole<Actor> {
}
public class GhostRole<Actor> {
}
The reason for the Generics is that every child class of Role knows its Actor as specified as possible to prevent casts. As you can see there are child classes of Role that need to construct specified versions of the Actor class (Hero, Enemy). But for the other classes (Animal, Ghost) I thought I could save the implementations of the constructActor() method by following my initial idea. That didn't work. Is there any other way of defining a default case in this scenario?