tempo temp
tempo temp

Reputation: 3

How to declare a method returning generic class?

I am new to java i have a set of classes which extends AbsEntity class.I have a abstract method which should return a Class object and that Class object should be E extends AbsEntity type...here is how i am trying to declare

abstract Class<E externds AbsEntity> E getAbsEntity();

This declaration through me error Actually i dont know hw to declare...A method whos return type should be Class type which is E extends AbsEntity Plz help

Upvotes: 0

Views: 68

Answers (3)

flopcoder
flopcoder

Reputation: 1175

You can use the following code:

static <E> E yourMethod(){
    AbsEntity absEntity = new AbsEntity();
    return (E)absEntity;
}

Upvotes: 0

anacron
anacron

Reputation: 6721

It should be declared before the return type, like so:

    abstract <E extends AbsEntity> Class<E> getAbsEntity();

Take a Look here at Oracle docs.

Hope this helps!

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

Something like this:

public <E extends AbsEntity> E yourMethod() {
    //yourObject
    return yourObject;
}

Upvotes: 1

Related Questions