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
?
Generics means parameterized types. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is called ...
It makes the code stable.Java Generics methods and classes, enables programmer with a single method declaration, a set of related methods, a set of related types. Generics also provide compile-time type safety which allows programmers to catch invalid types at compile time. Generic means parameterized types.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
Generics in Java is similar to templates in C++. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well. There are some fundamental differences between the two approaches to generic types. Generic Class Like C++, we use <> to specify parameter types in generic class creation.
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
.
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>> {}
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