horrorhorst
horrorhorst

Reputation: 3

Generic issue - inferred type does not conform to declared bound

We got the following class signatures:

car:

public class car<E extends Parts> [...]

parts:

public abstract class parts<E extends Stuff> [...]

public class Tire<T extends StoneTire> extends parts<T>[...]

stuff:

public abstract class Stuff [...]

public class Painting extends Parts [...]

[...]

We want to save the car in a Treeset < Parts< E>> (in the "Car"-class), but the compiler says nope :[ Because if we try to save a Tire in a City of Tools he cant find the StoneTire type in the bounds of Tool.

Upvotes: 0

Views: 898

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279930

Your method

public final <M extends Museum<E>> void addMuseum(final M museum)

has its own type parameter M, but it also depends on the class type parameter E. M is bound at the method invocation, but E is bound at the declaration of the instance (or of the expression).

You've got

toolCity.addMuseum(m1);

where toolCity seems to be an instance of

public class ToolCity extends City<Tool>
                                 // E

SO the method being invoked, fully bound, is

public final void addMuseum(final Museum<Tool> museum) ...

But your m1, from the exception message, seems to be a

Museum<StoneTool> m1 = ...

A Museum<StoneTool> is not a subtype of Museum<Tool> and can therefore not be used as an argument to something that expects the latter.

Upvotes: 2

Related Questions