Michael
Michael

Reputation: 33297

Generic Type of a Generic Type in Java?

I have an interface

public interface BWidgetObject<T> {
}

and I want to use this interface to create a new generic interface based on this type:

public interface BDataList<BWidgetObject> {}

The former gives a warning that the type T is hidden. The following give a compiler error:

public interface BDataList<BWidgetObject<T>> {}

How can I express BWidgetObject<T> as type parameter for BDataList?

Upvotes: 5

Views: 79

Answers (2)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

You can try with:

public interface BDataList<T extends BWidgetObject<?>> {}

Here we're specifying the the type T will be a BWidgetObject of a type we don't actually care about (and that's why we use a wildcard). We only care about T and the fact it will be a subtype of BWidgetObject.

Upvotes: 6

Bohemian
Bohemian

Reputation: 425033

Use a generic bound:

public interface BDataList<T extends BWidgetObject<?>> {}

Or if you need to type the widget explicitly, you need to create another sub-interface:

public interface BWidgetDataList<T> extends BDataList<BWidgetObject<T>> {}

Upvotes: 1

Related Questions