Can't create generic for an interface implementor

Délisson Junio

I'm trying to create a kind of service locator class, where there is a

Map<Integer, ? extends ISomething>

but I can't later do

myMap.put(0x00, new SomethingImplementor())

As I get a Error:(18, 33) java: incompatible types: org.sample.SomethingImplementor cannot be converted to capture#1 of ? extends ISomething

My class structure is as follows:

public interface ISomething {
    public void doSomething();
}

public class SomethingImplementor implements ISomething {
    @Override public void doSomething() {...}
}

Why can't I create this map and put values into it?

Marco Acierno

You don't need the wildcard at all.

You can directly use

Map<Integer, ISomething>

and you can implement every subclass of ISomething.

Anyway, to work with wildcards in this case you should use super. With extends you don't know what type it will be so you can't add anything to the map.

List is an example of a bounded wildcard. The ? stands for an unknown type, just like the wildcards we saw earlier. However, in this case, we know that this unknown type is in fact a subtype of Shape. (Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the upper bound of the wildcard.

There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into shapes in the body of the method. For instance, this is not allowed:

You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.

Wildcards: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't create generic for an interface implementor

From Dev

Why can't a Java Generic implement an Interface?

From Dev

Typescript Can't implement generic function interface

From Dev

Can't add a concrete instance of a generic interface to a generic collection

From Dev

Can't add a concrete instance of a generic interface to a generic collection

From Dev

How can I pass an argument to one implementor of an Interface but not the others (without passing it in the constructor)?

From Dev

Create generic property in interface

From Dev

Kotlin: Interface whereby the implementor must be a data class?

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

create an interface and its generic version

From Dev

Can I cast a generic array of T that implements an interface, to this specific interface array?

From Dev

Why can't I create an array of an inner class of a generic type?

From Dev

Why can't I create a generic bubbleSort with generator? (Java)

From Dev

Generic and Non-Generic Interface - List of T

From Dev

Check if a generic T implements an interface

From Dev

How can you bound the type of a generic interface to another generic interface?

From Dev

How can I create a covariant extension method on a generic interface in C#?

From Dev

Generic method where T implements Interface<T>

From Dev

Javassist: Create class that implements generic interface

From Dev

How to create generic Map interface in TypeScript

From Dev

Create generic Interface restricted to own class

From Dev

Why can't I convert concrete type to interface while using generic constraints

From Dev

Can't convert concrete type to generic version of its Interface in C#

From Dev

Reflection can't get the actual parameter type of a method that overrides from a generic-typed interface?

From Dev

How to correctly create a bounded generic class that extends a generic interface in Java

From Dev

Can we create a Dictionary with Generic?

From Dev

Is it possible to create a generic type T[A <: C[U], B <: C[U], U] that can be used as T[A, B]?

From Dev

WCF - Can the callback interface specified as a generic one?

From Dev

Can't import interface

Related Related

  1. 1

    Can't create generic for an interface implementor

  2. 2

    Why can't a Java Generic implement an Interface?

  3. 3

    Typescript Can't implement generic function interface

  4. 4

    Can't add a concrete instance of a generic interface to a generic collection

  5. 5

    Can't add a concrete instance of a generic interface to a generic collection

  6. 6

    How can I pass an argument to one implementor of an Interface but not the others (without passing it in the constructor)?

  7. 7

    Create generic property in interface

  8. 8

    Kotlin: Interface whereby the implementor must be a data class?

  9. 9

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  10. 10

    create an interface and its generic version

  11. 11

    Can I cast a generic array of T that implements an interface, to this specific interface array?

  12. 12

    Why can't I create an array of an inner class of a generic type?

  13. 13

    Why can't I create a generic bubbleSort with generator? (Java)

  14. 14

    Generic and Non-Generic Interface - List of T

  15. 15

    Check if a generic T implements an interface

  16. 16

    How can you bound the type of a generic interface to another generic interface?

  17. 17

    How can I create a covariant extension method on a generic interface in C#?

  18. 18

    Generic method where T implements Interface<T>

  19. 19

    Javassist: Create class that implements generic interface

  20. 20

    How to create generic Map interface in TypeScript

  21. 21

    Create generic Interface restricted to own class

  22. 22

    Why can't I convert concrete type to interface while using generic constraints

  23. 23

    Can't convert concrete type to generic version of its Interface in C#

  24. 24

    Reflection can't get the actual parameter type of a method that overrides from a generic-typed interface?

  25. 25

    How to correctly create a bounded generic class that extends a generic interface in Java

  26. 26

    Can we create a Dictionary with Generic?

  27. 27

    Is it possible to create a generic type T[A <: C[U], B <: C[U], U] that can be used as T[A, B]?

  28. 28

    WCF - Can the callback interface specified as a generic one?

  29. 29

    Can't import interface

HotTag

Archive