Generally speaking, you can write generic constructors. We recently had a question about them and how they can be useful . So you can provide a constructor that takes as an argument a class representing a class that extends some specific other class and implements the Bar interface:
class Foo<B extends Bar> { B[] bs; Foo(Class<B> clazz, Stream<B> stream) { // General ctor bs = someFunctionOf(clazz, stream); } private B[] someFunctionOf(Class<B> clazz, Stream<B> stream) { return null; } <T extends SomeClass & Bar> Foo(Class<T> clazz) { // ... } }
But this is not quite the way you want, because the bounds of an argument of type T constructor must be explicit types. Typical variables, such as a type parameter of class B , are not served, and without a way to connect T to B special generic constructor cannot invoke a common constructor.
But you can do this using the factory method instead of a special constructor:
class Foo<B extends Bar> { B[] bs; Foo(Class<B> clazz, Stream<B> stream) {
John bollinger
source share