0

This is my first time dealing with inheritance and I'm trying to create a subclass of the ArrayList class, but I'm getting stopped pretty early on.

import java.util.ArrayList;

/**
 * extending to ArrayList
 */
public class SortedArrayList<E> extends ArrayList<E>
{



/**
 * Inheriting the supers constructors
 */
public SortedArrayList()
{
   super();

  }


 public void add(){

  SortedArrayList.add(); // testing out inherited method.
}


  }

however when I try to compile this I get an error, "non-static method add() can not be referenced from a static context"

What am I doing wrong?

5 Answers 5

2

Just as a matter of style ... I don't think that you really want to do this.

While ArrayList is documented for inheritance, this is not typical. Subclassing across package boundaries from base classes that are not expressly designed for inheritance can make your code fragile, error prone, and insecure. The reason for this is that inheritance breaks encapsulation. Your subclass becomes dependent upon the implementation details of the base class.

If you are writing new code, I recommend the following stylistic alternatives to subclassing ArrayList:

  • Subclass AbstractList instead (which IS documented and designed for inheritance)
  • Make ArrayList an implementation detail of SortedArrayList by making it a private field (i.e. favor composition over inheritance); adding instrumentation to an existing class is typically best done via composition and forwarding method calls to the contained instance.

These recommendations are very much in line with the advice given in Effective Java, 2nd Ed.

Sign up to request clarification or add additional context in comments.

Comments

1

There is no no-arg add method in ArrayList to call. If there were, then you would write super.add();. But overriding a method just to call super.add() and nothing else is the same as not overriding the method at all and letting your subclass inherit the method.

I suspect you want something like this:

@Override
public void add(E e) {
    // Write code here to find the index where it belongs, in an "index" variable.
    // Then call super.add with the proper index so it remains sorted.
    super.add(index, e);
}

Comments

0

The correct way to invoke a method on your parent class is

super.add();

Comments

0

Couple of problems:

The error message gives you a major clue; static members are associated with the class, rather than an object instance, which is typically done by referencing class name specifically, much as you have done (a simple example would be MyClass.myStaticMethod()). However, the add() method in your case is non-static; it is an instance method.

Its unclear what you're trying to do, specifically, in this method, presently. If you remove the SortedArrayList portion of the method call, you'll end up in an infinite recursive loop, because it'll continuously call itself.

I assume you're attempting to call the superclass's add method, however you're missing the method signature. If you are attempting to override the ArrayList add method, you should note that there are two version; if you want to override these methods, they should contain the same signature, and look more like this:

First Version

public boolean add(E e){
    // your code
}

Or the second version

public void add(int index, E element){
    // your code
}

Comments

0

When you write SortedArrayList.add(); you are calling the static method "add" of "SortedArrayList" class... but it doesn't exist. If you have to call the add() method of superclass (non-static), then you should use super.add().

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.