Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple restrictions on generic type based on super and sub classes in java

I have a generic list class that implements a particular interface.

The list items in the interface also implement the same interface.

public abstract class List<T extends SomeInterface> implements SomeInterface
{
    protected LinkedList<T> m_list;

    ...
}

So now I want to make a subclass of this list that stays generic but limits the items to objects that implement the SearchListItem interface:

public interface SearchListItem
{
    public String getName();
}

Here's what I have for the SearchList class so far:

public abstract class SearchList<T extends SearchListItem> extends List<T>
{
    public T get(String name)
    {
        ...
    }

    ...
}

But of course this complains on the definition of the class:

Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends SomeInterface> of the type List<T>

So what do I need to put into the class declaration to say "SearchList extends the List class and has additional restrictions on the generic class type that includes both SomeInterface (in the base class) and SearchListItem as well"?

Please tell me if I can reword this to help explain it.

like image 788
Brad Avatar asked Nov 03 '10 09:11

Brad


1 Answers

Does this work?

public abstract class SearchList<T extends SomeInterface & SearchListItem> extends List<T>
like image 186
Landei Avatar answered Nov 15 '22 05:11

Landei