Calling (non public) methods from overriden method

82 views Asked by At

I am having an issue with override (something I am fairly new with), I will post my problem specifically.

I have made a class that extend ArrayList.

I have a few questions regarding what I have to do and regarding overrides.

Firstly, if ArrayList implements and extends all it needs to, will my new class which extends ArrayList also "automatically" extend and implement the necessary classes and interfaces. I am tempted to assume yes because I know you can only extend one class.

My second problems is about overrides.

I have overridden the method get() from:

@SuppressWarnings("unchecked")
E elementData(int index) {
    return (E) elementData[index];
}

/**
 * Returns the element at the specified position in this list.
 *
 * @param  index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index) {
    rangeCheck(index);

    return elementData(index);
}

to:

@Override
public E get(int index) {
    DoSomethingWithIndex...
    return elementData(index);
}

NOTE: I have included the code above get in the source. the E elementData...

I did not overwrite this though.

My problem is that I am told that elementData (the return of my overridden get method) cannot be found. Should I also override it? Is it not extended with ArrayList. I do not understand why it cannot be found.

3

There are 3 answers

4
aioobe On BEST ANSWER

Firstly, if ArrayList implements and extends all it needs to, will my new class which extends ArrayList also "automatically" extend and implement the necessary classes and interaces.

Yes. For example, the class below is perfectly valid:

public class MyList extends ArrayList<String> {
    // Empty!
}

No need to implement anything or mention any interfaces.

My problem is that I am told that elementData (the return of my overwritten get method) cannot be found.

That's right. elementData is private and can't be accessed in subclasses. Instead, use

@Override
public E get(int index) {
    DoSomethingWithIndex...
    return super.get(index);  // Delegate call to overridden method.
}

Lastly, extending ArrayList is rarely the right thing to do. You should almost always favor composition over inheritance. See for instance:

0
user3854270 On

I don't understand your second problem completely but for the first question the answer is: yes. Everything implemented with ArrayList is implemented with your subclass aswell. You only need to import the classes you want to use explicit in your code for example when you override something.

0
Powerlord On

You may want to Joshua Bloch's advice from Effective Java, Second Edition, Item 16: Favor Composition over inheritance.

That is, instead of extending ArrayList directly, create a class that wraps it that also implements List. That way, you also avoid some gotchas in the implementation that aren't documented.

In the example in said book, HashSet's addAll calls its add method, but isn't documented as doing so because it wasn't written with inheritance in mind.