What is the purpose of "&" in the generic type statement?

Asked

Viewed 112 times

10

In that code, the author creates a class as follows:

public abstract class GlyphIcon<T extends Enum<T> & GlyphIcons> extends Text { /* ... */ }

My question is to understand the meaning of this & in the following part:

<T extends Enum<T> & GlyphIcons>

From what I’ve noticed, GlyphIcons is an interface. So that & would play the same role as a implements? For example:

<T extends Enum<T> implements GlyphIcons>

That’s it (or almost it) ?

What is the "&" character when used in the generic type declaration?

1 answer

8


The type of parameter a generic type can handle can be limited by using the word extends(Bounded Type Parameters).

The constraint is made by indicating the class it extends and/or interfaces it implements.

The & is used to separate multiple limits.

If one of the limits is a class, it must be specified first.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.