Darius Miliauskas
Darius Miliauskas

Reputation: 3514

Java: adding a new method to the interface List<E>

Is there possible to add a new custom method to the well-known interface List in a way in Java? I tried to implement List interface to "public abstract class MyList", however, I would like to achieve it remaining the same name of the interface "List". For example, later implememnt in the following way:

List mylist = new ArrayList();
mylist.newmethod();

Upvotes: 1

Views: 1070

Answers (3)

eduardohl
eduardohl

Reputation: 1216

Here's what you want to do. I wouldn't recommend it though, it can be confusing to have two List classes.

public abstract class List implements java.util.List {
    public void myMethod() {
        ...
    }
}

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240898

You could extend it to another interface

interface MyList<E> extends List<E> {
    //add new methods here
}

Upvotes: 6

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

No, Java does not support such a feature. Use composition.

Upvotes: 6

Related Questions