Error adding subclass element to superclass ArrayList

I have a superclass with several 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>(); 

There are several errors that I see in Eclipse.
Firstly, any attempt to add a subclass object 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 in the code, when I try to apply 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 call clearly defined in the superclass that look undefined for the type. I am banging my head. I do not know what's the problem.

If you could possibly point me to some possible directions, I would be very obliged. I do not know if I have provided enough information, so please ask any questions that you think might be applicable.

Thanks in advance.

+4
source share
3 answers

After looking at all the other answers and your question, the only possible reason that comes to mind for this behavior is that you have two class files for SubClass1. One in which SubClass1 extends SuperClass and one in which it is absent. The class that is trying to insert SubClass1 into an ArrayList seems to use a later class file. For the same reason, superclass methods are not displayed in an instance of a subclass.

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

If the above approach does not solve the problem, it is also possible that your java source file and your .class file are not synchronized. Delete the bin folder in the eclipse project and re-create the project. You can also clean and build a project to make sure nothing is in sync.

+5
source

The following program works fine for me (works 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 SuperClass and SubClass1 to different classes and deleting the static identifier, I get the same output

Output:

 Test1 Test2 
+2
source

change this SubClass1 object; object = new SubClass1(parameters); SubClass1 object; object = new SubClass1(parameters); on the

 SuperClass object; object= new SubClass1(parameters); 
0
source

Source: https://habr.com/ru/post/1438228/


All Articles