8

I have been trying to understand whether it is possible to make a method which infers a generic type based on the return class and calls a static method of that generic type.

i.e. Below I create 2 classes both of which implement the getInstances and getAllInstances methods. I then attempt to create use the methods from a generic wrapper. It appears that the super class method is always being run regardless of the return type.

For example,

public class ParentClass {

    public ParentClass(){}

    public static <T extends ParentClass> T getInstance(){
        return (T) new ParentClass();
    }

    public static <T extends ParentClass> List<T> getAllInstances(){
        ArrayList<ParentClass> parents = new ArrayList<ParentClass>();

        for(int i=0;i<5;i++){
            parents.add(new ParentClass());
        }

        return (List<T>) parents;
    }

}

SubclassA

public class SubclassA extends ParentClass{

     public SubclassA(){}

    @SuppressWarnings("unchecked")
    public static SubclassA getInstance(){
         return new SubclassA();
    }

    @SuppressWarnings("unchecked")
    public static List<SubclassA> getAllInstances(){
        ArrayList<SubclassA> parents = new ArrayList<SubclassA>();

          for(int i=0;i<5;i++){
             parents.add(new SubclassA());
          }

         return parents;
      }
 }

Wrapper - Shows the problem

 public class Wrapper {

    public Wrapper(){
        // ... some other stuff
    }

    public <T extends ParentClass> T getInstance(){
        return T.getInstance();
    }

    public <T extends ParentClass> List<T> getAllInstances(){
        return T.getAllInstances();
    }

    public static void main(String... args){
        Wrapper wrapper = new Wrapper();

        SubclassA subclassA = wrapper.getInstance();
        ParentClass parentClass = wrapper.getInstance();

        System.out.println(subclassA.getClass().getName());
        System.out.println(parentClass.getClass().getName());
    }

 }

When running Wrapper I get the following error:

Exception in thread "main" java.lang.ClassCastException: ParentClass cannot be cast to SubclassA at Wrapper.main(Wrapper.java:20)

Can I do this in Java?

2
  • don't understand what you expect here.... you're calling a static method on a class, it's executed. why should it delegate the call to a subclass ? and how to determine which one ? Commented Apr 18, 2013 at 10:02
  • Well the hope was it would determine the which sub class to delegate to by inferring from the return type. Now I understand that you can't override static methods it makes sense. Commented Apr 18, 2013 at 11:14

3 Answers 3

2

static methods will not be override.This static methods will belongs to Class level

Sign up to request clarification or add additional context in comments.

Comments

1

Your approach is incorrect, there is no notion of static Inheritance in Java as inheritance always is in context of Object level. Only member methods (non-static) can be inherited by sub classes having appropriate access modifiers.

Update: Further to add, in your scenario Factory pattern seems more suitable than adjusting generics for getting/constructing specific class objects.

Comments

1

No, you can't do it like this. To make it work you can pass the Class object:

public class ParentClassUtils

    public static <T extends ParentClass> T getInstance(Class<T> clazz) {
        return clazz.newInstance();
    }

    public static <T extends ParentClass> List<T> getAllInstances(Class<T> clazz) {
        List<T> list = new ArrayList<T>();
        for (int i = 0; i < 5; i++) {
            list.add(getInstance(clazz));
        }
        return list;
    }
}

Also, in your example you have equivalent static methods in parent classes and subclasses. The subclass methods don't override the parent class methods, they merely hide them, and this almost certainly isn't what you want. This utility class approach above gets around this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.