Java Overloading not working?
Posted by amlosty@reddit | learnprogramming | View on Reddit | 2 comments
i'm trying to overload the add method with another one that only takes in one parameter but i just keep getting this error
The method add(E) of type ArrayList<E> must override or implement a supertype methodJava(67109498)
import java.util.Iterator;
public interface List<E> extends Iterable <E>{
/**
* Returns the number of elements in the list.
* @return number of elements in the list
*/
int size();
/**
* Tests whether the list is empty.
* @return true if the list is empty, false otherwise
*/
boolean isEmpty();
/**
* Returns (but does not remove) the element at index i.
* @param i the index of the element to return
* @return the element at the specified index
* @throws IndexOutOfBoundsException if the index is negative or greater than size()-1
*/
E get(int i) throws IndexOutOfBoundsException;
/**
* Replaces the element at the specified index, and returns the element previously stored.
* @param i the index of the element to replace
* @param e the new element to be stored
* @return the previously stored element
* @throws IndexOutOfBoundsException if the index is negative or greater than size()-1
*/
E set(int i, E e) throws IndexOutOfBoundsException;
/**
* Inserts the given element at the specified index of the list, shifting all
* subsequent elements in the list one position further to make room.
* @param i the index at which the new element should be stored
* @param e the new element to be stored
* @throws IndexOutOfBoundsException if the index is negative or greater than size()
*/
void add(int i, E e) throws IndexOutOfBoundsException;
/**
* Removes and returns the element at the given index, shifting all subsequent
* elements in the list one position closer to the front.
* @param i the index of the element to be removed
* @return the element that had be stored at the given index
* @throws IndexOutOfBoundsException if the index is negative or greater than size()
*/
E remove(int i) throws IndexOutOfBoundsException;
/**
* Returns an iterator of the elements stored in the list.
* @return iterator of the list's elements
*/
Iterator<E> iterator();
}
@Override
public void add(E e) throws IndexOutOfBoundsException {
if (size == data.length) {
data = upSizeArray(data); // Resize the array when full
}
data[size] = e;
size++;
}
@Override
public void add(int i, E e) throws IndexOutOfBoundsException {
checkIndex(i, size + 1);
if (size == data.length)
throw new IllegalStateException("array full");
data = upSizeArray(data);
for (int k = size - 1; k >= i; k--) {
data[k + 1] = data[k];
}
data[i] = e;
size++;
desrtfx@reddit
There is no
add(E e)
method in the interface and hence, you cannot override it.Also, be careful with the naming of classes/interfaces.
List
is a built in Interface injava.util.List
.Generally, you should avoid naming your classes/interfaces the same as existing ones, no matter what package they are in.
high_throughput@reddit
If you're adding a new method that does not override anything, then you should not use the @Override annotation