I was wondering if it is possible to have something like this:
public class foo<T...>
so you can call the class like
Foo<Object0>
Foo<Object0, Object1>
Foo<Object0, Object1, Object2>
With Object 0, 1 and 2 different types, like Integer, Float, String and so on. Is this possible, or would I have to write a class for each lenght of generic types? If this would be possible, how would I handle the different types?
No, you can't have that. The best you can do is public class Foo<T extends SomeClassOrInterface>
. So in your example with Integer
and Float
you can define public class Foo<T extends Number>
.
You can also specify that T must implement more than one interface.
The syntax public class Foo<T extends SomeInterface1 & SomeInterface2>
is valid, with &
meaning AND
.
Unfortunately the syntax public class Foo<T extends SomeInterface1 | SomeInterface2>
with |
meaning OR
is not possible.
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