I'm looking for a way to return a new instance of a type which extends an abstract
class in Java, ideally without reflection from a static method.
So I want be able to call:
SomeClass anInstanceOfSomeClass = Builder.build<SomeClass>()
I hoped I could do something like:
public class Builder {
public static <T extends SomeBaseClass> T build() {
return new T(); // cannot instantiate type T
}
}
I've seen a few examples using Supplier
but they tend to be based on either a non static, using a generic class rather than a method and/or require passing in the ctor
as a param which seems a bit more complex syntactically.
Feels like this should be simple but coming from C# I'm not too familiar with the best way to achieve this in Java. Any thoughts appreciated.