Here is my goal: I want to have an argument that needs to be two separate types at the same time.
The goal here is to extract two different types from a single object using just generics (no casting or instanceof).
My idea is to have a third type parameter that extends the other two.
class DualType<A, B> {
A a;
B b;
<T extends A & B> DualType(T obj) {
a = obj;
b = obj;
}
}
This does not work. Since java does not support multiple inheritance, and there isn't a way the compiler can guarantee that B isn't another class, we get an error.
My question is this: Is there some way to define a type parameter that constrains it to only be used with interfaces? I would assume you would be able to use implements rather than extends but as we all know, only super and extends are valid in type parameter declarations. Is there a workaround for this? Or is it simply not possible?
The thing I am trying to achieve here isn't for some practical purpose, but I rather would just like to know if something like this is possible at all in Java?