Reputation: 3514
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
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
Reputation: 240898
You could extend it to another interface
interface MyList<E> extends List<E> {
//add new methods here
}
Upvotes: 6
Reputation: 279970
No, Java does not support such a feature. Use composition.
Upvotes: 6