2

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
Max
  • 859
  • 5
  • 10
  • I considered and tried using super to call the method elementData but it did not work and I am not sure it would work. edit: super did work, I was simply calling it incorrectly. I was calling it on the elementData method when I should have called it on the get method. – Max Jun 10 '15 at 21:30

3 Answers3

2

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:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thanks a lot! Worked great. I love how quickly questions are answered on here! :) – Max Jun 10 '15 at 21:32
  • Out of curiosity, why did you accept the answer that talks about what to import and didn't cover your second part? – aioobe Jun 11 '15 at 05:29
  • Haha, sorry I'm quite new here, can you only accept one answer. I accepted all answer which I found helpful. But looking the ticks, it seems that it if I accept a second answer, it will untick the first one. – Max Jun 11 '15 at 06:26
  • 1
    Right :-) upvote all answers you find useful (also on other questions than your own), accept the answer you find to be the best :-) – aioobe Jun 11 '15 at 06:29
1

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.

user3854270
  • 68
  • 1
  • 8
1

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175