Reputation: 303
This is a simplified, fabricated scenario intended to demonstrate my problem.
I want to create an interface MarketingScheme<T>
that will have a collection of participants implementing the interface MarketingCustomers<? super T>
. As an example, I might have a marketing scheme designed for Vehicles, of which a marketing customer for Cars might be a participant. I have another interface, Manufacturer<R> extends MarketingCustomer <R>
, so a manufacturer of cars is also a marketing customer of cars.
My MarketingScheme interface defines a single method, public void addParticipant(MarketingCustomer<?super T> customer);
. I would expect to be able to add a MarketingCustomer<Car>
to a MarketingScheme<Vehicle>
, assuming Car was a subclass of Vehicle. There may be many schemes, so I should be able to add the same manufacturer to a MarketingScheme<DurableGood>
, assuming Vehicle extends DurableGood
If I create a ManufacturerImpl and pass in a MarketingScheme as follows:
public class ManufacturerImpl<R> implements Manufacturer<R>
{
public ManufacturerImpl(MarketingScheme<? super R> scheme) {
super();
scheme.addParticipant(this);
}
}
... I get the error
The method addParticipant(MarketingCustomer<? super capture#1-of ? super R>) in the type MarketingScheme<capture#1-of ? super R> is not applicable for the arguments (ManufacturerImpl<R>)
I have read up on Generic Methods (helperMethods) and tried to implement one, but I could not get it right. I have considered adding an additional type declarations to the interfaces, but none of them compiled or seemed appropriate in my model. I couldn't find any casts that worked inside of my addParticipant
call either.
My interfaces are defines as follows ...
public interface MarketingScheme<T>
{
public void addParticipant(MarketingCustomer<? super T> customer);
}
public interface MarketingCustomer<S>
{
}
public interface Manufacturer<R> extends MarketingCustomer<R>
{
}
Your answers greatly appreciated. Thank you.
Upvotes: 1
Views: 3568
Reputation: 1459
change it for
public interface MarketingScheme <T> {
public void addParticipant(MarketingCustomer<<? extends T> customer);
}
Upvotes: 0
Reputation: 2692
You need to change the type signature of addParticipant
:
public void addParticipant(MarketingCustomer<? extends T> customer);
This is because customer's type parameter should be a subclass, not a superclass, of T
.
Upvotes: 1