1

We know that ArrayList.add returns boolean which indicates true or false.

Now a customization is required which says when we try and add null in arrayList it returns a custom string. Lets say for example it goes like this

public String add(Element e) {
   if (e == null) {
      return "StackOverflow";
   }
}

Now when I see that hierarchy List interface and Collection both have return type of add as boolean, there is no provision of overriding it.

please suggest how to proceed.

7
  • 2
    You simply can't override it, as there is no such method in ArrayList. You can overload it, though; but you'd need another parameter to the method to disambiguate it from boolean add(E). (Or you could give it a different name). Commented Dec 22, 2016 at 11:51
  • 2
    It sounds like in the context of your overall application, this shouldn't be the job of the list's add method. Commented Dec 22, 2016 at 11:52
  • 1
    "returns boolean which indicates true or false." 🤔 Commented Dec 22, 2016 at 11:54
  • 1
    @AndyTurner "which is why I say ..." sorry, overlooked that... Commented Dec 22, 2016 at 11:58
  • 1
    The first thing that comes to my mind is: why do you want to do that? This sounds like some weird idea to probably solve a completely different problem. Commented Dec 22, 2016 at 12:00

3 Answers 3

4

How about using:

String someMethod(Element e) {
     if (e == null) {
         return "StackOverflow";
     }
     theList.add(element);
     return "someString"
}
Sign up to request clarification or add additional context in comments.

Comments

1

An alternative is to extend ArrayList (or another list implementation). But you need another name of the method, such as addElement.

public class MyArrayList<E> extends ArrayList<E> {
    public String addElement(E e) {
        if(e == null){
            return "StackOverflow";
        } else{
            super.add(e);
            return "Other";
        }
    }
}

@Test
public void addElement() throws Exception {
    MyArrayList<String> strings = new MyArrayList<>();
    assertEquals("StackOverflow", strings.addElement(null));
    assertEquals("Other", strings.addElement("other"));
}

5 Comments

but that would still leave you with the option of inserting null values with the normal add method.
Then the wrapper from fasseg is an good alternative. If you really MUST use add. But a wrapper is still a wrapper.
And you can extend add aswell, and throw exception if it is used with null
you can´t override add and throw an exception as that would make you change the method signature (adding a throws clause). You can simply make it ignore null values and return false, but the missing feedback would make it look weird.
You can throw an unchecked exception, such as RuntimeException (NPE). To avoid that someone actually uses add. But I would recommend to avoid doing so and instead use a wrapper if you actually just want to limit people the available methods.
1

You cannot override the add method with the same signature and return a different type. Since the add(T t) method is defined in the Collection interface as returning a boolean, all methods in the type hierarchy with the same signature must return a boolean.

You can however:

Change the Signature by adding more arguments:

public class MyList<T> extends ArrayList<T> {

    public String add(T t, String success, String error){
        if (add(t)) {
            return success;
        } else {
            return error;
        }
    }  
}

or

Change the Signature by using a different method name:

public class MyList<T> extends ArrayList<T> {

    public String addItem(T t){
        if (add(t)) {
            return "success";
        } else {
            return "error";
        }
    }  
}

or

Use a wrapper class that uses aggregation of an ArrayList to do the underlying operations. But you will have to implement all the methods get, add, size, etc

public static class MyArrayList<T> {
    private List<T> list;

    public MyArrayList() {
        this.list = new ArrayList<T>();
    }

    public String add(T t) {
        if (list.add(t)) {
            return "success";
        } else {
            return "error";
        }
    }

    public T get(int index) {
        return list.get(index);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.