nebuch
nebuch

Reputation: 7055

Java actual arguments don't match formal arguments, but they do?

I have a class Player that extends Entity:

Player:

public class Player extends Entity {
    public Player(char initIcon, int initX, int initY) {
        //empty constructor
    }
...

Entity:

public Entity(char initIcon, int initX, int initY) {
        icon = initIcon;
        x = initX;
        y = initY;
    }
...

This is pretty much what you'd expect, but on compile I get an error that reads

Player.java:2: error: constructor Entity in class Entity cannot be applied to the given types:
    public Player(char initIcon, int initX, int initY)
required: char,int,int
found: no arguments
reason: actual and formal argument lists differ in length

But it clearly does have the required arguments. What's going on here? Thanks!

Upvotes: 6

Views: 4813

Answers (5)

Jatin Khurana
Jatin Khurana

Reputation: 1175

When Child class inherit the parent class then default constructor of the parent class is called by default. In the above case you have defined the parametric constructor in the Parent class so default is not supplied by the JVM and your child class is calling the parent default constructor which doesn't exist there. Either specify the default constructor in the Parent class or call the Parametric constructor of the parent by using super.

public class Player extends Entity {
public Player()
{}
public Player(char initIcon, int initX, int initY) {
    //empty constructor
}

OR

public Player
(char initIcon, int initX, int initY) {
super(initIcon, initX, initY);
}

Upvotes: 0

Vinze
Vinze

Reputation: 2539

There is no explicit call to a super constructor (as shown in other answers or below) so the VM will use an implicit 0-arg constructor... but this constructor does not exist. So you have to do an explicit call to a valid super constructor :

 public class Player extends Entity {
    public Player(char initIcon, int initX, int initY) {
        super(initIcon,initX,initY);
    }

Upvotes: 2

PermGenError
PermGenError

Reputation: 46408

Your super class constructor has 3-arguments and doesn't seem to have an empty constructor. Thus your subclass constructor should make an explicit call to the super class constructor passing the values.

public class Player extends Entity {
    public Player(char initIcon, int initX, int initY) {
        //empty constructor
        super(initIcon,initX,initY);
    }
...

Upvotes: 7

Philipp
Philipp

Reputation: 69663

You need to call the constructor of the base class explicitely from the constructor of the extending class. You do that like that:

public class Player extends Entity {
    public Player(char initIcon, int initX, int initY) {
        super(initIcon, initX, initY);
        // rest of player-specific constructor
    }

Upvotes: 2

Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

You need to initialize super class by call its constructor with super

public Player(char initIcon, int initX, int initY) {
    super(initIcon, initX, initY);
}

Upvotes: 14

Related Questions