I have some base class Entity. I want to allow users of Entity to create instances of Entity that extend some given super class T (as if Entitys declaration was Class Entity extends T). I’m not aware of what T actually is, so I want Entity to be modular in that respect. If that’s not possible I’m ok with Entity implementing a given interface T as well.
Things like public class Entity<T> extends T and public class Entity<T> implements T do not work. (“Cannot refer to the type parameter T as a supertype”).
So my questions are:
1) is this at all possible to achieve in java ? some other language ?
2) if so, how ?
3) usually when java puts up hurdles like that it means something is wrong with my design, so how would you create this functionality (I guess what I’m looking for is basically multiple inheritance from Entity and T, can this be done ?).
EDIT: clarification – I want to achieve mixin type behavior while requiring as little as possible from the caller (creating a subclass of T which delegates calls to an Entity instance would demand way too much from the caller). is there no other way to do this ?
Java doesn’t support multiple inheritance; a given class can’t be a direct subclass of more than one class.
However, if
Entityis an interface, you can do it using an intersection bound.For example, to code a method that accepts an instance that is a subclass (not necessarily a direct subclass) of
SomeClassand that implementsEntity: