I am trying to figure out how to get generics to jump through hoops.
I have:
interface Root { }
interface Middle extends Root { }
class Type implements Root { }
And many "Subtype" classes:
class Subtype1 extends Type implements Middle { }
class Subtype2 extends Type implements Middle { }
...
What I want is to declare a class with two type parameters T
and S
, where T
is bound by Type
and S
is bound by T
and Middle
.
I can't see a way with generics to ensure that S
extends T
AND implements Middle
.
What I want is something like:
class Handler<T extends Root, S extends T, S extends Middle>;
or
class Handler<T extends Root, S extends <T extends Middle>>;
But of course neither are legal. Maybe there is some magic I am missing?
Handler<T extends Root, S extends Subtype>
isn't enough? The only difference is thatT
could possibly be a more specific type thanS
, that's it. – BankbookHandler<T extends Root, S extends Subtype>
means that Handler is bound to a single Subtype. I have several which means that Handler is no longer generic across Subtypes. In reality my code has half a dozen Subtypes which would mean an equal number of Handlers bound to the specific Subtype. – Glycoside