Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a generic type of a subclass within it's abstract superclass?

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

like image 503
Markus Avatar asked Jun 29 '10 08:06

Markus


People also ask

Can subclass be abstract if superclass is concrete?

A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but may have an abstract subclass like GeometricObject . A subclass may override a method from its superclass to declare it abstract.

Is abstract class generic?

Abstract ClassNow generics seem cool but they really shine when used with abstract classes. An abstract class is a class that itself is never intended to be instantiated, instead they are used to pass properties to sub classes via inheritance.

Can a superclass be abstract?

We can treat an abstract class as a superclass and extend it; its subclasses can override some or all of its inherited abstract methods. If through this overriding a subclass contains no more abstract methods, that class is concrete (and we can construct objects directly from it).

What is the generic class type called?

Generic Classes These classes are known as parameterized classes or parameterized types because they accept one or more parameters.


1 Answers

I think its possible. I saw this was being used in the DAO patterns along with generics. e.g. Consider classes:

public class A {}
public class B extends A {}

And your generic class:

  import java.lang.reflect.ParameterizedType;
  public abstract class Test<T extends A> {

     private Class<T> theType;

     public Test()  {
        theType = (Class<T>) (
               (ParameterizedType) getClass().getGenericSuperclass())
              .getActualTypeArguments()[0];
     }

     // this method will always return the type that extends class "A"
     public Class<T> getTheType()   {
        return theType;
     }

     public void printType() {
        Class<T> clazz = getTheType();
        System.out.println(clazz);
     }
  }

You can have a class Test1 that extends Test with class B (it extends A)

  public class Test1 extends Test<B>  {

     public static void main(String[] args) {
        Test1 t = new Test1();

        Class<B> clazz = t.getTheType();

        System.out.println(clazz); // will print 'class B'
        System.out.println(printType()); // will print 'class B'
     }
  }
like image 100
naikus Avatar answered Oct 28 '22 13:10

naikus