mvairavan
mvairavan

Reputation: 129

Generic Types in Java with ineterface

public class ResultContainer<T implements java.io.Serializable>
implements java.io.Serializable{
 int errorCode;
 T result;
 /* ... Few Other variables ... */
}

whats wrong in this code, I basically need a generic type(like a wrapper) for storing all my Results which contains the actual result with some other informations. Here i need both the actual Result class (i.e, 'T' here) and the ResultContainer Class to be serilizable.

How do i do this?

Upvotes: 0

Views: 1180

Answers (4)

PermGenError
PermGenError

Reputation: 46428

When you have interfaces in generics, you still have to use the extends keyword. you shouldnt use implements keyword in generics.

public class ResultContainer<T implements java.io.Serializable>
implements java.io.Serializable{

should be

public class ResultContainer<T extends java.io.Serializable>
implements java.io.Serializable{

Upvotes: 3

corlettk
corlettk

Reputation: 13574

Just a simple mistake... you have to say extends instead of implements in the generic-type-constraint... a but of a language-kludge if you ask me ;-)

import java.io.Serializable;

public class ResultContainer<TResult extends Serializable> implements Serializable
{
    private static final long serialVersionUID = 1L;
    int errorCode;
    TResult result;
}

It might pay you to review the generics tutorial right now, just to brush up your skills, as I presume you'll be using generics pretty extensively in this project.

Going through a tut is a lot quicker than asking all-those-fiddly-facts individually online.

Cheers. Keith.

Upvotes: 0

Mick&#228;el A.
Mick&#228;el A.

Reputation: 9387

You have to write

public class ResultContainer<T extends java.io.Serializable>

An implementation is considered as an inheritance because you inherits the methods of the interface.

Upvotes: 1

thedayofcondor
thedayofcondor

Reputation: 3876

You have to use:

public class ResultContainer<T extends java.io.Serializable>

Upvotes: 3

Related Questions