简体   繁体   中英

Error adding subclass item to ArrayList of superclass

I have a superclass with a couple of subclasses.

public abstract class SuperClass {
}

public class SubClass1 extends SuperClass {
}

I created an ArrayList to contain objects of type SuperClass.

private ArrayList<SuperClass> list = new ArrayList<SuperClass>();

The errors I'm seeing in Eclipse are several in number.
First, any attempt to add an object of a subclass has an error:

SubClass1 object;
object = new SubClass1(parameters);

list.add(object)  //I get error: The method add(SuperClass) in the type 
                  //ArrayList<SuperClass> is not applicable for the arguments 
                  //(SubClass1)

Later on in the code, when I try to cast one type to another, I get even more problems:

for (SuperClass obj : list){
    if (obj instanceof SubClass1){ //This gets an error like this:
         ....                      //Incompatible conditional operand types
    }                              // SuperClass and SubClass1

Not to mention that there are some methods that I'm calling that are clearly defined in the superclass that come up as undefined for type. I'm banging my head here. I have absolutely no idea what the problem could be.

If you folks could maybe point me in some possible directions, I'd be much obliged. I don't know if I've even supplied enough information, so please ask any questions that you think might be applicable.

Thanks in advance.

After taking a look at all the other answers and your question, the only possible reason that comes to mind for this kind of behavior is that you have two class files for SubClass1. One in which SubClass1 extends SuperClass and one in which it doesn't. The class that tries to insert a SubClass1 in the ArrayList seems to be using the later class file. For the same reason, the super class methods are not showing up on the subclass instance.

Check your imports and make sure you are using the correct version of SubClass1.

If the above approach doesn't solve the problem, It is also possible that your java source file and your .class file are out of sync. Delete the bin folder in the eclipse project and build the project again. You can also clean and build the project to make sure nothing is out of sync.

Following program works fine for me (running on JRE 1.6)

public class Test {
    public static abstract class SuperClass {
    }

    public static class SubClass1 extends SuperClass {
        public SubClass1() {

        }
    }

    public static void main(String[] args) {
        ArrayList<SuperClass> list = new ArrayList<SuperClass>();
        SubClass1 object;
        object = new SubClass1();
        System.out.println("Test1");
        list.add(object); 
        for (SuperClass obj : list) {
            if (obj instanceof SubClass1) { 
                System.out.println("Test2");
            }
        }
    }
}

Edit: Even after moving the SuperClass and SubClass1 to different classes and removing the static identifier I get the same output

Output:

Test1
Test2

change this SubClass1 object; object = new SubClass1(parameters); SubClass1 object; object = new SubClass1(parameters); to

SuperClass object;
object= new SubClass1(parameters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM