czetsuya
czetsuya

Reputation: 5043

Java extending generics

Is this possible?

class A<T extends Service<E extends Entity>>

It's because I want to get the type of Service and Entity. Or any other way around to do it?

Currently I have an abstract method that sets the Service but if I can do it in parameter then much better.

I'm also wondering how can I pass the parameters in a base class:

I have ClassA that extends SubBaseClass1 that extends BaseClass1.

So:

class SubBaseClass1<E extends Entity, P extends Service<E>> { }

In BaseClass, I want to know the type of P and E.

Another question, if I have a method getBaseClass from another class, how will I specify the return type?:

public BaseClass<E extends Entity, T extends Service<E>> getBaseClass() { }

Is not working.

Found the answer:

public BaseClass<? extends IEntity, ? extends Service<?>> getBaseClass() { }

Upvotes: 2

Views: 108

Answers (1)

Russell Zahniser
Russell Zahniser

Reputation: 16354

You would declare that like this:

class A<E extends Entity, T extends Service<E>>

Then you could have, for example:

A<Foo, Bar> a;

... where Foo is a subclass of Entity and Bar is a subclass of Service<Foo>.

Upvotes: 6

Related Questions