Adexe Sabina
Adexe Sabina

Reputation: 13

How to properly override ArrayList .remove() method

I need to override add() and remove() methods from my ArrayList of clients.

If I add or delete a client, the sum of the orders of the clients that are on the list must be automatically updated.

Actually I have something like this:

public class Path {
  private ArrayList<Client> clients = new ArrayList<Client>() {
    @Override
    public boolean add(Client e) {
      boolean allOk = super.add(e);
      if (allOk)
        calcTotalOrder();
      return allOk;
    }
    @Override
    public boolean remove(Client e) {
      boolean allOk = super.remove(e);
      if (allOk)
        calcTotalOrder();
      return allOk;
    }
  };
}

I have no problem when overriding add() method and it actually works exactly as I want, but not the same luck with remove().

I get the following error: "The method remove(Client) of type new ArrayList(){} must override or implement a supertype method".

The proposal of Eclipse is to eliminate @Override

What am I doing wrong?

Upvotes: 1

Views: 2915

Answers (5)

Veselin Davidov
Veselin Davidov

Reputation: 7081

You can change that remove to Object and it will work because remove takes an object. You will have a problem though if someone uses a different way to add object. Like addAll() or creating an ArrayList from a collection constructor.

In order to use that instead of modifying ArrayList methods you can extend the ArrayList to be like "ArrayList that can calculate order". Something like:

class ArrayListThatCalculatesTotalOrder extends ArrayList<Client>{
     .... your private calcTotalOrder() methods is here ....
    public TotalOrder getTotalOrder(){
        calcTotalOrder();
        return totalOrder;
    }
}

This way you will be able to use it as a list freely but you can also get the calculated total order with a simple getter in a standard way. Or something similar to that depending on what exactly do you need. It is better to add behavior than to modify the existing one.

Upvotes: 0

Elroy Jetson
Elroy Jetson

Reputation: 968

When overriding methods, the signatures must be the same. Otherwise, you're not overriding. You can simply go to the List implementation and look at the methods you're attempting to override. In your case, it's remove and add. You will find that the method signature does not take your Client object as a parameter. Instead, it takes Object as a parameter.

In order to override, you must maintain the methods signature. Object is the super class to all java objects, and the error "must override or implement a supertype method" is simply telling you that you're using the @Override annotation, without actually overriding anything.

Upvotes: 0

khachik
khachik

Reputation: 28703

If you take a look at java.util.ArrayList, it has two remove methods:

public boolean remove(Object o)

public boolean remove(int index)

You need to override these methods. Also, list iterators can remove elements as well, if you need full removal support, you have to override the list iterator.

Upvotes: 1

William
William

Reputation: 347

Replace

public boolean remove(Client e)

by

public boolean remove(Object e)

Upvotes: 1

Piotr Wilkin
Piotr Wilkin

Reputation: 3501

For historical reasons, the remove() method is not polymorphic, i.e. it must support removing an arbitrary Object and not an object of type T.

However, you have to remember to also override the respective iterator methods (since calling remove() is not the only way to add things into an ArrayList), as well as all the other methods that modify elements in the collection (i.e. set()).

Upvotes: 1

Related Questions