Suppose I have three isolated public classes (no IS-A relationship) A, B and C. I want to define a field in C such that it\'s type can either be A or B.
Currently I\'m a
Make the constructor private:
private C(T param){
And then provide static factory methods to create instances of particular types:
public static C create(T param) {
return new C<>(param);
}
public static C create(T param) {
return new C<>(param);
}
This doesn't prevent you from using the type C
; you just can't create an instance of it.