I have an abstract class in java:
private abstract class Importable<T> {
// ...
public abstract List<T> validateData(List<Path> paths);
public abstract void importData(List<T> data);
// ...
}
private static class MyImportable extends Importable<String>{
public List<String> validateData(List<Path> paths){
return // valid ArrayList<String>;
}
public void importData(List<String> data){
// do stuff with data
}
}
Now, if I do this:
public Importable importable;
// ...
importable = new MyImportable(); // MyImportable extends Importable<String>
calling this works:
importData(validateData(myPaths));
But I don't like the raw Importable
, so I added <?>
public Importable<?> importable;
doing so throws error:
The method importData(List<capture#5-of ?>) in the type ImportController.Importable<capture#5-of ?> is not applicable for the arguments (List<capture#6-of ?>)
while executing importData(validateData(myPaths));
and eclipse suggests me to cast to List<?>
, which did nothing to solve the problem
Is there something I'm missing?
EDIT: sorry if I wasn't clear, here is the actual usage
I will have many class that extends Importable with different types:
Importable importable;
switch(condition){
case 1:
importable= // Importable<MyType1>
break;
case 2:
importable= // Importable<MyType2>
// ....
}
importData(validateData(myPaths));
EDIT2:
I intentionally separate importData
and validateData
because some other class might want to validateData
only
If matters: I'm using eclipse 4.3 w/ Java 8 on Windows 7 x64
Replace the generic argument ?
with String
EDIT (taking the edited question into account):
I think that the following would be a better design:
switch(condition){
case 1:
validateAndImport(someImportable, paths); //Importable<MyType1>
break;
case 2:
validateAndImport(someOtherImportable, paths); //Importable<MyType1>
}
...
<T> void validateAndImport(Importable<T> importable, List<Path> paths) {
importable.importData(importable.validateData(myPaths))
}
This will solve your problem
public Importable<String> importable;
// ...
importable = new MyImportable(); // MyImportable extends Importable<String>
as you know MyImportable
extends Importable<String>
--EDIT--
as per you updated question, please try this one
abstract class Importable<T> {
public abstract List<T> validateData(List<Path> paths);
public abstract void importData(List<? extends Object> list);
}
and use
public Importable<? extends Object> importable;
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.