4

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.

7
  • 1
    Superclass is not a type of subclass, but the reverse is true Commented Oct 7, 2012 at 6:30
  • 3
    I am able to add subtype object into ArrayList that you have declared. Commented Oct 7, 2012 at 6:38
  • Huh... I wonder where my real error is, then... Commented Oct 7, 2012 at 7:08
  • This is definitely one of those beginner gotchas. See my answer. Commented Oct 7, 2012 at 8:17
  • @SirYancy.. I think you should see the answer of bot and follow the advice he gave.. He has pointed out the most proabable reason.. :) Commented Oct 7, 2012 at 8:34

3 Answers 3

5

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.

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

7 Comments

There's a part of me that is absolutely terrified to report that your very reasonable answer does not seem to solve my problem. All told, I have 1 superclass that is defined as abstract and three (different) subclasses that extend it. Whenever I try to add an object from one of the subclasses to an ArrayList<superclass> I get a java error. And it also refuses to acknowledge any perfectly valid instanceof operations. I'm stumped completely.
Quick question though: what do you mean by checking my imports? Do I need to import classes that are in the same package?
No you don't need to import classes from the same package. If you have any import statements in your class that inserts values into the list, just make sure the right classes are being imported. Are you absolutely sure there is only one version of all your subclasses and the super class as well?
If I said "Yes, I'm absolutely sure," would that help or just make this whole thing more confusing?
You need to realize that errors like this don't magically occur out of nowhere without a mistake on your part. I suggest you start over fresh if this is just a sample program you are writing to learn some concepts. Create a new project. Create the exact same classes again. Things should work as expected.
|
2

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

3 Comments

Do you think that the fact that your classes are static and nested makes any difference? I should add that mine are in separate class files.
I moved my SuperClass and SubClass1 to different classes, and removed the static identifier. Still works for me well :)
@SirYancy.. True..I'm also not facing nay problem in that.. Its working fine for me.. And it has to work.. This is allowed.. You see, a List<Animal> can always hold a Dog because a Dog is an animal at the end.. You should check for any other class with the same name.. Probably that is getting used..
0

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

SuperClass object;
object= new SubClass1(parameters);

4 Comments

This will lead to Object slicing!
Not aware of what it is, time to learn object slicing :D Thanks for new info anyway
@vandey.. How will it solve the problemm.. You have just changed the code to actually store the superclass reference to a superclass ArrayList.. This will anyhow happen.. This is not the concern..
It shows OP the better practice of OOP. The code itself does not have any problem, it runs fine.

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.