1
private static final Map<String, ? extends TrackingInterface<TrackResponseObject>> map = new HashMap<>();
    static {
        map.put("dunzo", new DunzoShipment());
        map.put("self", new SelfShipment());
    }
public class DunzoShipment implements TrackingInterface<TrackResponseObject>,java.util.function.Supplier<TrackingInterface<TrackResponseObject>> { 
//body
}

while puting the object into map i am getting an error:

The method put(String, capture#1-of ? extends TrackingInterface) in the type Map<String,capture#1-of ? extends TrackingInterface> is not applicable for the arguments (String, DunzoShipment)

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Hemant
  • 57
  • 1
  • 7
  • If both of the shipment class are implementing the same `TrackingInterface` then there is no need for the wildcard there. – Silverfang Aug 27 '20 at 10:22

1 Answers1

2
? extends TrackingInterface

means "something which extends TrackingInterface". We don't know what that something is, so we can't be sure that DunzoShipment is one of them. That's why you can't insert into the map.

PECS: producer extends, consumer super. This is a consumer (via put), so you cannot use extends.

Just remove the ? extends part and it will compile.

Michael
  • 41,989
  • 11
  • 82
  • 128