Generics Puzzler
Asked Answered
G

2

8

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?

Glycoside answered 27/3, 2013 at 23:40 Comment(6)
You cannot do this. You can have multiple bounds but they must be 0-1 Class plus multiple interfaces. You cannot have a Generic type in a multiple bound situation. I suspect this restriction was kept because to allow type in multiple bounds would create a lot of complexity to the syntax checker, for what is a rare edge case.Partitive
related: https://mcmap.net/q/327649/-java-generics-make-generic-to-extends-2-interfacesBankbook
Thanks. That was my read as well but figured it was possible I was missing something in the spec. Casts it is!Glycoside
Can you explain why declaring Handler<T extends Root, S extends Subtype> isn't enough? The only difference is that T could possibly be a more specific type than S, that's it.Bankbook
Handler<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
I made some edits to try and clarify your problem - could you please review my changes and let me know if anything is off?Bankbook
M
2

Try introducing an abstract class that extends SubType and implements Middle, so its type can be used in Handler.

abstract class MiddleSubtype extends Subtype implements Middle { }

Then,

class Handler<T extends Root, S extends MiddleSubtype> {  //...

EDIT: Following the update to the question, the same idea would look like:

abstract class MiddleType extends Type implements Middle { }

class Subtype1 extends MiddleType { }
class Subtype2 extends MiddleType { }
...

class Handler<T extends Root, S extends MiddleType> {  //...
Meijer answered 27/3, 2013 at 23:47 Comment(5)
Note that this is different from T extends Root, S extends T ... though, since S is no longer bound by T.Bankbook
Yes and it ties Handler to one subtype. I actually have a bunch of subtypes which would mean a bunch of Handlers as well which is exactly what I was trying to avoid.Glycoside
@NickPalmer See my edit to this answer - I think this is close to what rgettman was suggesting.Bankbook
This does in fact work for the revised question posed and so I am accepting it. Unfortunately my real type hierarchy is even MORE complicated and so I am stuck with casting. Call it a case of over simplifying for the question and the restrictions on Generics being problematic.Glycoside
@NickPalmer If you have time to put together a follow-up question, I'm sure people would be interested.Bankbook
E
0

Or you could make S generic itself:

interface SInt<A,B> {}
class Handler<T extends Root, S extends SInt<T, Middle>>{}
Edp answered 6/1, 2014 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.