2

What I want to do is the following:

public class DBManager<T extends Type , Q extends Query>{
    T create(T t);
    Collection<T> read(Q query);
    T update(T t);
}

However, I only want instantiations of this class where T and Q are related. For instance, if T is AppleType, then I want Q to be AppleQuery and not PearQuery.

Is there a way to do this, or produce a similar behavior?

I tried Q extends T & Query, but that isn't allowed.

9
  • Even if you could do that, I don't think you'd get very far on the implementation side of things. Commented Mar 11, 2013 at 19:15
  • Why not? It's absolutely possible that you're correct, but I haven't worked with generics in Java before, and I'm not immediately seeing the reason. Commented Mar 11, 2013 at 19:16
  • How much control do you have over Type and Query? If they are from your library you could wrap valid pairs in a "endorsed type" object. Commented Mar 11, 2013 at 19:16
  • Are Type and Query your own classes? If so, can you share the details of each? Commented Mar 11, 2013 at 19:16
  • What is the relationship (if any) between the Type and Query classes? Commented Mar 11, 2013 at 19:19

4 Answers 4

4

You cannot do that unless the types themselves have some kind of relation in the type system.

Since I don't know exactly what you want to do, it's hard to recommend something with precision, but it sounds like you might want to do something like this:

public abstract class Type {
    ...
}

public class AppleType extends Type {
    ...
}

public abstract class Query<T extends Type> {
    public abstract T doQuery(); /* Or so? */
}

public class AppleQuery extends Query<AppleType> {
    public AppleType doQuery() {...}
}

With such a formal relation from Query to Type, you could then define your DBManager as such:

public class DBManager<T extends Type, Q extends Query<T>> {
    T read(Q id);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Since Q is not used in return types, you don't need it. Simply

interface DbManager<T extends Type>

    T read(Query<? extends T> query)

where

interface Type

interface Query<T extends Type>

Comments

0

You could make everything generic:

public class Type<Q extends Query<? extends Type<Q>>
public class Query<T extends Type<? extends Query<T>>

public class DBManager<T extends Type<Q> , Q extends Query<T>>

3 Comments

You probable want public class Type<T extends Type<T, Q>, Q extends Query<Q, T>>, etc., to be useful.
@TomHawtin-tackline: Yes; I was thinking of suggesting that. I'm pretty sure I went through this whole rigmarole in a recent SO answer, but I can't find it.
Well here's an example of the same kind of rigmarole: stackoverflow.com/questions/9423047/cyclical-generics-try-2
0

There isn't multiple inheritance in Java. Why not just to do this?

public class Generic{
 // Generic atributes and methods.
}

public class ClassA extends Generic{
 // ClassA atributes and methods overwriting, if need it, the extended methods from Generic class.
}

public class ClassB extends Generic{
 // ClassB atributes and methods overwriting, if need it, the extended methods from Generic class.
}

... // You can instantiate so many classes as you need. You can use the factory pattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.