简体   繁体   中英

Trying to sub-type classes using generics

I am having problems trying to create a sub-type class, and calling its superconstructor with an ArrayList of objects of a certain generic type. I am trying to get this code to work. I want my SubTypes to ONLY accept a list of GenericObjects that are of type X .

    public class SubType extends SuperType{

        public SubType(ArrayList<GenericObject<Y>> g) {
            super(g);
        }
    }

    public class SuperType {

        public ArrayList<GenericObject<?>> data = new ArrayList<GenericObject<? extends X>>();

        public SuperType(ArrayList<GenericObject<? extends X>> series) {

            data.addAll(series);
        }
    }


    class GenericObject<T extends X>{}
    class X{};
    class Y extends X{};

I get an error where the super(g) is called. It says that

The constructor TestGenerics.SuperType(ArrayList>) is undefined

Thats kind of strange because I thought that Generics allowed this kind of thing.

If I don't pass through an ArrayList of my generic object, and just pass the generic object through then it is OK.

public class SubType extends SuperType{

    public SubType(GenericObject<Y> g) {
        super(g);
    }
}

public class SuperType {

    public ArrayList<GenericObject<?>> data = new ArrayList<GenericObject<? extends X>>();

    public SuperType(GenericObject<? extends X> series) {

        data.add(series);
    }
}

class GenericObject<T extends X>{
}

class X{};
class Y extends X{};

The above works perfectly. Is there any way to get the first example to work?

Your problem is that ArrayList<Child> does not extend ArrayList<Parent> even if Child extends Parent . However, you were on the right track before, because ArrayList<Child> does extend ArrayList<? extends Parent> ArrayList<? extends Parent> .

You just need to repeat your ? extends ... ? extends ... pattern one more time , like this:

public SuperType(ArrayList<? extends GenericObject<? extends X>> series) {
    data.addAll(series);
}

This compiles fine for me.


UPDATE : The full code would look like this:

public class SubType extends SuperType{

    public SubType(ArrayList<GenericObject<Y>> g) {
        super(g);
    }
}

public class SuperType {

    public ArrayList<GenericObject<?>> data = new ArrayList<GenericObject<? extends X>>();

    public SuperType(ArrayList<? extends GenericObject<? extends X>> series) {

        data.addAll(series);
    }
}


class GenericObject<T extends X>{}
class X{};
class Y extends X{};

Are you perhaps in search of this solution?

class SubType extends SuperType<Y> {

    public SubType(List<GenericObject<Y>> g) {
        super(g);
    }
}

class SuperType<T extends X> {

    public List<GenericObject<T>> data = new ArrayList<GenericObject<T>>();

    public SuperType(List<GenericObject<T>> series) {
        data.addAll(series);
    }
}

class GenericObject<T extends X>{}
class X{};
class Y extends X{};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM