Java Generics argument mismatch

hashbrown

I have an Interface Worker like below:

public interface Worker<T> {

    public void doAllTasks (List<? extends T> tasks);
}

There is an Abstract Class that uses above interface,

public abstract class AbstractWorker<T> implements Worker<T> {
    ...
    public abstract void doAllTasks (List<? extends T> tasks);
    ...
}

And there is a concrete Worker extending from this Abstract class,

public class ConcreteWorker<T extends Task> extends AbstractWorker<T>  {

    @Override
    public void doAllTasks(List<? extends T> tasks) {
    ....
    }
}

Notice the Task in the above class definition? That is an interface. SpecialTask is a concrete class that implements Task interface. Now once this structure is there, I wish to use this classes in my main Application like below:

Class Application {
    public AbstractWorker<? extends Task> myWorker;

    main() {
        ....
        List<SpecialTask> listOfTasks = new ArrayList<SpecialTask>();
        myWorker = new ConcreteWorker<SpecialTask>();
        myWorker.doAllTasks (listOfTasks); <-------------- ERROR!!!
    }
}

IDE shows me the following error in the above marked line:

The method doAllTasks(List<? extends capture#3-of extends Task>) in the type
AbstractWorker<capture#3-of extends Task> is not applicable for the 
arguments (List<SpecialTask>)

What I am trying to do is - in my main Application I want to first declare the Abstract version of my Worker first and later, based on some logic I want to instantiate a concrete version of that worker. I do not know before hand which particular implementation of Task I will need (so it could be the SpecialTask or AnotherSpecialTask etc. - assuming both of them implements Task)

Honestly being new to Java, I could not make any head or tail of that error message. Can someone please show me where am I going wrong?

Jon Skeet

The problem is that the type of myWorker is just AbstractWorker<? extends Task> - in other words, it doesn't know that the type argument is really SpecialTask. It could be an AbstractWorker<SomeOtherTask>, which wouldn't know what to do with a List<SpecialTask>.

If you only ever want myWorker to hold a worker that can deal with SpecialTask, you can just use:

public AbstractWorker<SpecialTask> myWorker;

That's still not depending on ConcreteWorker, but it is saying that it's definitely not just an AbstractWorker<SomeOtherTask> for example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java generics: Bound mismatch

From Dev

Java Stream Generics Type Mismatch

From Dev

Type mismatch with Java generics and anonymous class

From Dev

Java/Scala Bounded Generics and type inference mismatch

From Dev

Java generics - type mismatch from T to T

From Dev

Java Generics Bound Mismatch Compilation Error

From Dev

Java generics bound mismatch recursive type

From Dev

Java argument type mismatch while reflection

From Dev

Java Generics Type Argument as Variable possible?

From Dev

Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Java generics: Bound mismatch.Looking for the valid substitute

From Dev

Java generics : Type mismatch: cannot convert from Integer to K

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Java generics: Bound mismatch.Looking for the valid substitute

From Java

Bound mismatch error on generics type argument for a generic class that extends another generic class

From Dev

Scala type mismatch with generics

From Dev

o:graphicImage throws java.lang.IllegalArgumentException: argument type mismatch

From Dev

java.lang.IllegalArgumentException: argument type mismatch on string array

From Dev

o:graphicImage throws java.lang.IllegalArgumentException: argument type mismatch

From Dev

Java Method.invoke() throwing IllegalArgumentException: argument type mismatch

From Dev

Java interface generics, return type depending on argument generic

From Dev

Java generics - method with "extends" typed collection parameter rejects valid argument?

From Dev

Returning the same type As Supplied as an Argument - Java8 Generics

From Dev

Kind mismatch with GHC.Generics

From Dev

Scala: Type mismatch with nested generics

From Dev

Kind mismatch with GHC.Generics

From Dev

Kotlin generics mismatch in a class hierarchy

From Dev

Kotlin generics inheritance - Type mismatch

Related Related

  1. 1

    Java generics: Bound mismatch

  2. 2

    Java Stream Generics Type Mismatch

  3. 3

    Type mismatch with Java generics and anonymous class

  4. 4

    Java/Scala Bounded Generics and type inference mismatch

  5. 5

    Java generics - type mismatch from T to T

  6. 6

    Java Generics Bound Mismatch Compilation Error

  7. 7

    Java generics bound mismatch recursive type

  8. 8

    Java argument type mismatch while reflection

  9. 9

    Java Generics Type Argument as Variable possible?

  10. 10

    Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type

  11. 11

    Java generics. Type mismatch: cannot convert from object to

  12. 12

    Java generics: Bound mismatch.Looking for the valid substitute

  13. 13

    Java generics : Type mismatch: cannot convert from Integer to K

  14. 14

    Java generics. Type mismatch: cannot convert from object to

  15. 15

    Java generics: Bound mismatch.Looking for the valid substitute

  16. 16

    Bound mismatch error on generics type argument for a generic class that extends another generic class

  17. 17

    Scala type mismatch with generics

  18. 18

    o:graphicImage throws java.lang.IllegalArgumentException: argument type mismatch

  19. 19

    java.lang.IllegalArgumentException: argument type mismatch on string array

  20. 20

    o:graphicImage throws java.lang.IllegalArgumentException: argument type mismatch

  21. 21

    Java Method.invoke() throwing IllegalArgumentException: argument type mismatch

  22. 22

    Java interface generics, return type depending on argument generic

  23. 23

    Java generics - method with "extends" typed collection parameter rejects valid argument?

  24. 24

    Returning the same type As Supplied as an Argument - Java8 Generics

  25. 25

    Kind mismatch with GHC.Generics

  26. 26

    Scala: Type mismatch with nested generics

  27. 27

    Kind mismatch with GHC.Generics

  28. 28

    Kotlin generics mismatch in a class hierarchy

  29. 29

    Kotlin generics inheritance - Type mismatch

HotTag

Archive