Java ArrayList inheritance issue (looks like namespace collision)

I am new to Java (actually spending three days), but I have to write my own cross-platform editor application as an interface to my database. Actually everything is working fine, but I have a weird package error. inb4 3 years of Python and AS3 programming.

I am trying to extend java.util.ArrayList and hit the override of the add method . The code looks something like this:

package myxmleditor;

public class BarsList<EditorXMLObject> extends 
    java.util.ArrayList<EditorXMLObject> {

@Override
public boolean add(EditorXMLObject element) {
    editorGUI.addEditorPane(element); // error here
    return super.add(element);
}

    public EditorGUIInterface editorGUI = null;
}

BarsList , EditorGUIInterface and EditorXMLObject are in the myxmleditor package . AddEditorPane Method -

EditorGUIInterface.addEditorPane(EditorXMLObject element)

NetBeans shows me an error:

method addEditorPane in class myxmleditor.TsukasaXMLEditGUI cannot be applied to given types;
  required: **myxmleditor.EditorXMLObject**  
  found: **EditorXMLObject**  
  reason: actual argument EditorXMLObject cannot be converted to myxmleditor.EditorXMLObject by method invocation conversion
+3
source share
3

BarsList . BarsList EditorXMLObject, :

public class BarsList extends java.util.ArrayList<EditorXMLObject>

, ArrayList, :

public class BarsList<T> extends java.util.ArrayList<T>

+7

, ArrayList. ArrayList BarsList, .

( ArrayList). , ArrayList ( hertzsprung) , ArrayList. , remove, addAll ?

, , addEditorPane . , .

+5

Collections of subclasses are dangerous because, for example, you don’t know whether or not addAll () adds the delegate to add () inside the superclass (this is explained later in Effective Java , p. 71).

If you can avoid this, it would be better to create a transport collection; Google Guava ForwardingList can be useful here.

+2
source

All Articles