gonzalomelov
gonzalomelov

Reputation: 981

Java Generics extending List

I have the following classes:

public interface ServiceSynchronizableEntity {
    ....
}

public class BaseListResponse<T> {
    ....
}

public class BaseSynchronizableListResponse<T extends ServiceSynchronizableEntity> extends BaseListResponse<T> {
    ....
}

public class MySecondClass implements ServiceSynchronizableEntity {
    ....
}

public class MyFirstClass extends BaseSynchronizableListResponse<MySecondClass> {
    ....
}

public class What<S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity>> {

}

Then I use it as:

What what = new What<MyFirstClass>();

When I want to use MyFirstClass as a type parameter which extend BaseListResponse<ServiceSynchronizableEntity>> it shows me

Main.java:26: error: type argument MyFirstClass is not within bounds of type-variable S
    What what = new What<MyFirstClass>();
                         ^
where S is a type-variable: S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity> declared in class What

What is wrong?

Edit: One class was missing. Look at: http://ideone.com/D4snKP

Thanks!

Upvotes: 0

Views: 100

Answers (1)

nivekastoreth
nivekastoreth

Reputation: 1427

The solution I previously posted (now deleted) was similar in that it required a change to the generic signature.

The important change is going from:

class What<S extends BaseSynchronizableListResponse<ServiceSynchronizableEntity>> {}

to:

class What<S extends BaseSynchronizableListResponse<? extends ServiceSynchronizableEntity>> {}

See http://ideone.com/wyXvcl for the fixed version

Upvotes: 1

Related Questions