I'm trying to compile this new class :
public class WindowedGame
extends GameContainer<GameType extends Game<Graphics2D>> {
...
}
This class extends the class :
public abstract class GameContainer<GameType extends Game<?>> {
....
}
Can you suggest me a correction or explain to me why I get the error :
Unexpected bounds
Thanks!
A bound is a constraint on the type of a type parameter. Bounds use the extends keyword and some new syntax to limit the parameter types that may be applied to a generic type. In the case of a generic class, the bounds simply limit the type that may be supplied to instantiate it.
If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class. These are known as bounded-types in generics in Java.
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number . Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).
A type parameter can have multiple bounds.
GameType
is the generic type parameter name, so it cannot be in the extends
clause.
If WindowedGame
should be generic, define it as
public class WindowedGame<GameType extends Game<Graphics2D>>
extends GameContainer<GameType> {
...
}
If WindowedGame
shouldn't be generic, perhaps you meant to define it as
public class WindowedGame
extends GameContainer<Game<Graphics2D>> {
...
}
BTW, the naming convention for generic type parameter names is often a single upper case character (T
, E
, etc...). It would be less confusing if instead of GameType
you write T
.
public class WindowedGame<T extends Game<Graphics2D>>
extends GameContainer<T> {
...
}
public abstract class GameContainer<T extends Game<?>> {
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With