Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected bound

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!

like image 669
hlapointe Avatar asked Dec 28 '16 07:12

hlapointe


People also ask

What does Bound mean in Java?

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.

What are bounded types?

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.

How do you use bound 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).

Can a parameterized type have several bounds?

A type parameter can have multiple bounds.


1 Answers

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<?>> {
    ....
}
like image 87
Eran Avatar answered Oct 20 '22 04:10

Eran