Generic generic type in Java?

I have an interface

public interface BWidgetObject<T> { } 

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

 public interface BDataList<BWidgetObject> {} 

The first warns that type T is hidden. The following are compiler errors:

 public interface BDataList<BWidgetObject<T>> {} 

How to express BWidgetObject<T> as a type parameter for a BDataList ?

+5
source share
2 answers

You may try:

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

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

+6
source

Use a common border:

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

Or, if you need to explicitly enter a widget, you need to create another sub-interface:

 public interface BWidgetDataList<T> extends BDataList<BWidgetObject<T>> {} 
+1
source

Source: https://habr.com/ru/post/1216275/


All Articles