I am trying to parameter a class with a type that is whether A or B and nothing else.
Something like
final class ChildClass<A | B> extends MotherClass {
This way I can create a ChildClass<A>
or a ChildClass<B>
but nothing else.
Is it possible in Java?
Edit:
I must specify that I am using Java 8 and that I have no control of classes A and B (which are classes of the Android framework I am working with).
Unfortunately is not. Search for term union types in java
or java either type
.
The most known case of union types in Java is catching multiple exceptions, which is built-in into language, however it would be awful to abuse exceptions for your own types.
Here is also interesting workaround.
With the addition of this JEP, there is a rather interesting way to do it now.
You can create an interface that is sealed
:
sealed interface F permits A, B {
default String name() {
return "F";
}
}
add the generic type that is going to be bounded by this interface:
static class WithGenerics<T extends F> {
private final T t;
public WithGenerics(T t) {
this.t = t;
}
public void findName() {
System.out.println(t.name());
}
}
And finally create the two classes A
and B
:
static final class A implements F {
.....
}
static final class B implements F {
.....
}
And you have effectively said that only A
and B
can be passed to :
WithGenerics<A> a = new WithGenerics<>(new A());
a.findName();
WithGenerics<B> b = new WithGenerics<>(new B());
b.findName();
because there can be no type C
that can potentially implement F
.
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