0

I have an abstract class with abstract method "Action"

public abstract class BaseAnt {
    public BaseAnt(String name, int food, int water) {
        /.../
    }

    public abstract void action ();   
}

The class worker extends it:

   public class WorkerAnt extends BaseAnt {
    public WorkerAnt() {
        /.../
    }

    public void action() {
        AppStat.FOOD += mAge*0.1 + 10;
    }
}

And I'm trying to make loop

    public ArrayList<BaseAnt> antsArray;
        for (int i = 0; i < AppStat.antList.size(); i++) {
        if (AppStat.antList.get(i).getName() == "Worker") {
            AppStat.antList.get(i).action();
        }
    }

But I can't call the method action! How to fix it?

Error:

The method action() is undefined for the type BaseAnt

Karlo
  • 1,630
  • 4
  • 33
  • 57
Sunstrike
  • 456
  • 1
  • 6
  • 22
  • What do you mean with "I can't call"? Do you get a compilation error? Do you get an exception when running? Is the method simply not called? – isnot2bad Nov 11 '13 at 16:48
  • "The method action() is undefined for the type BaseAnt" – Sunstrike Nov 11 '13 at 16:48
  • You have made an error by writing `== "Worker"` when you should have `.equals("Worker")`. However, this is not the cause of your compiler error. – Kevin Panko Nov 11 '13 at 17:04
  • 1
    for one, your comparison are flawed, which is what i was pointing at. as for the undefined method, the only explanation i see so far is that you are using a different BaseAnt class, and have a import issue. – njzk2 Nov 11 '13 at 17:06

2 Answers2

0

Strings in Java are compared in this manner:

String foo = "foo";
String bar = "bar";
String foo2 = "foo";

foo.compareTo(bar); // false
foo.compareTo(foo2); // true

When you compare (foo == foo2), you're actually comparing references, which is a no-no, in your case.

Edit: I've just written a tiny test example:

import java.util.ArrayList;
import java.util.List;


public class BaseAntQuestion {

    public static void main(String[] args) {
        new BaseAntQuestion();
    }

    public BaseAntQuestion() {
        List<BaseAnt> ants = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            ants.add(new WorkerAnt());
        }

        for (BaseAnt ant : ants) {
            ant.action();
        }
    }

    public abstract class BaseAnt {
        public abstract void action();
    }

    public class WorkerAnt extends BaseAnt {

        @Override
        public void action() {
            System.out.println("Action!!!");
        }

    }

}

And it works just fine.

Can you please post the AppStat class? My gut is telling me you may have conflicing BaseAnt imports there.

ioreskovic
  • 5,531
  • 5
  • 39
  • 70
0

I think you are using and accessing your abstract class fine in the code you have posted.

I would suggest that the problem is somewhere in your AppStat class. In particular look at AppStat.antList and look to see if you may have accidentally created or referenced a different BaseAnt class.

As mentioned by others there is a problem with the way you are comparing strings, which you will want to look at, but that is not what is causing the error you are seeing.

Ken
  • 654
  • 8
  • 18