0

I have a class called CreatureAi with the following code.

public class CreatureAi {
    public Creature creature;

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.

I have a class called PlayerAi which extends it.

public class PlayerAi extends CreatureAi {
    private FieldOfView fov;
    private Player player;

    public PlayerAi(Player player, FieldOfView fov) {
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
    }
    // These are the only constructors.

However, Netbeans gives me this error.

constructer CreatureAi in class CreatureAi cannot be applied to the given types.
required: Creature
found: No arguements
reason: Actual and formal lists differ in length.

Why am I getting this error?

3
  • 5
    super(creature) in first line, you don't have constructor with no-args in CreatureAi Commented Dec 6, 2013 at 1:11
  • @nachokk Where did I write super(creature)? Commented Dec 6, 2013 at 1:13
  • 1
    Check the Java Language reference about object construction: docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4 Commented Dec 6, 2013 at 1:15

1 Answer 1

3

When you write your subclass, implicit calls super() that is in super type constructor.

public PlayerAi(Player player, FieldOfView fov) {
        super(); // this call "father" constructor
        this.player = player;
        this.player.setCreatureAi(this);
        this.fov = fov;
}

As you show in your code , your base class doesn't have no-arg constructor. So your child is not valid. You have to call one valid super constructor.

public PlayerAi(Player player, FieldOfView fov) {
            super(//??creature); // you have to pass something here
            this.player = player;
            this.player.setCreatureAi(this);
            this.fov = fov;
    }

Alternative if you can modify your CreatureAi, you can add default no-args constructor.

public class CreatureAi {
    private Creature creature;

    public CreatureAi(){}

    public CreatureAi(Creature creature) {
        this.creature = creature;
        this.creature.setCreatureAi(this);
    }
    // There's more, I'm shortening it.
Sign up to request clarification or add additional context in comments.

13 Comments

So my child class automatically calls my parent constructor?
Basically, you need to have super(aPlayer, aFov) as the first instruction in PlayerAi constructor, or make a no-arg constructor for CreatureAi.
Yes, every time a class in initialized, all parents constructor are called, in order, from Object to the actual class, through all the class hierarchy.
@AnubianNoob One thing you may want to consider is making an Ai interface that both CreatureAi and PlayerAi implement instead of trying to make PlayerAi inherit from CreatureAi. I've found that I rarely use inheritance in practice, and in my opinion, my code is much cleaner because of it.
It might be simplest for the newbie to remove the parms from the constructors and just use setters to set values, after the object is created. Less "elegant", but easier to understand.
|

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.