Within my code a have the following abstract superclass
public abstract class AbstractClass<Type extends A> {...}
and some child classes like
public class ChildClassA extends AbstractClass<GenericTypeA> {...}
public class ChildClassB extends AbstractClass<GenericTypeB> {...}
I'm searching for an elegant way how I can use the generic type of the child classes (GenericTypeA, GenericTypeB, ...) inside the abstract class in a generic way.
To solve this problem I currently defined the method
protected abstract Class<Type> getGenericTypeClass();
in my abstract class and implemented the method
@Override
protected Class<GenericType> getGenericTypeClass() {
return GenericType.class;
}
in every child class.
Is it possible to get the generic type of the child classes in my abstract class without implementing this helper method?
BR,
Markus
I'm not sure I fully understand your question -
<Type>
is the generic type of the subclass, even when it's being expressed in the abstract class. For example, if your abstract superclass defines a method:and you instantiate a
ChildClassA
, you'll only be able to callaugment
with an instance ofGenericTypeA
.Now if you want a class literal, then you'll need to provide the method as you indicated. But if you just want the generic parameter, you don't need to do anything special.
I think its possible. I saw this was being used in the DAO patterns along with generics. e.g. Consider classes:
And your generic class:
You can have a class Test1 that extends Test with class B (it extends A)