2

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() { }
1

1 Answer 1

6

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>.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks that's the answer I've been looking for. Btw, do you know how I can pass the same parameter to a parent class? I'll add more description above.
Generic type parameters cannot be passed as formal parameters to methods. Due to type erasure, they exist only at compile time. The run time system has no knowledge of generic types, if that's what you are asking.
Thanks got it, I've tried to pass them as constructor arguments seems like it's working. Now I'm trying if I can inject the service.

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.