简体   繁体   中英

Why can't a method take a Collection<subClass> when the method's signature is defined as Collection<class>

I have a method that takes a list of SResource objects

public static List<STriple> listTriples(List<SResource> subjects){
//... do stuff
}

Why can't I do this

List<IndexResource> resultsAsList = new ArrayList<IndexResource>();
    resultsAsList.addAll(allResults.keySet()); // I could possible not use lists and just use sets and therefore get rid of this line, but that is a different issue
List<STriple> triples = new ArrayList<STriple>();
    triples = TriplesDao.listTriples(resultsAsList);

(The compiler tells me I have to make triples use SResource objects.)

When IndexResource is a subclass of SResource

public class IndexResource extends SResource{ 
// .... class code here
}

I would have thought this has to be possible, so maybe I am doing something else wrong. I can post more code if you suggest it.

You can do it, using wildcards :

public static List<STriple> listTriples(List<? extends SResource> subjects){
    //... do stuff
}

The new declaration uses a bounded wildcard , which says that the generic parameter will be either an SResource , or a type that extends it.

In exchange for accepting the List<> this way, "do stuff" can't include inserting into subjects . If you're just reading from the subjects in the method, then this change should get you the results you want.

EDIT : To see why wildcards are needed, consider this (illegal in Java) code:

List<String> strings = new ArrayList<String>();
List<Object> objList = string; // Not actually legal, even though string "is an" object
objList.add(new Integer(3)); // Oh no! We've put an Integer into an ArrayList<String>!

That's obviously not typesafe. With wilcards, though, you can do this:

List<String> strings = new ArrayList<String>();
string.add("Hello");
List<? extends Object> objList = strings; // Works!
objList.add(new Integer(3)); // Compile-time error due to the wildcard restriction

你不能这样做,因为仿制药不是“协变”List<Integer>不是一个子类的List<Number>虽然Integer是一个子类的Number

For those who are unable to add wildcards, this should work.

List<Integer> list = new ArrayList<Integer>();
new ArrayList<Number>(list);

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