user219882
user219882

Reputation: 15844

Java generics - is it possible to restrict T to be Serializable?

Is it possible to make something like this? I know that implements cannot be in the <>, but I want to restrict the T to be Serializable somehow.

public class Clazz<T implements Serializable> {
    ...
}

Upvotes: 21

Views: 9861

Answers (3)

Perception
Perception

Reputation: 80633

Yes, just use extends instead of implements.

Upvotes: 4

Heisenbug
Heisenbug

Reputation: 39194

public class Clazz<T extends Serializable> {
    ...
}

Upvotes: 32

Ingo
Ingo

Reputation: 36339

Just use extends instead of implements.

Upvotes: 6

Related Questions