Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 860
nebuch Avatar asked Dec 19 '12 15:12

nebuch


People also ask

What is actual and formal argument lists differ in length?

Formal arguments are defined in the definition of the method. In the below example, x and y are formal arguments. Actual arguments refer to the variables we used in the method call. In the below example, length and width are the actual arguments.

How to fix constructor in class cannot be Applied to Given Types?

Another way to fix the code is to define a constructor in the SpecificThing class that takes an argument and calls the superclass constructor, and then change the code in main to use that constructor instead, as shown below.

What is actual and formal arguments?

Actual Parameters. Formal Parameters. When a function is called, the values (expressions) that are passed in the function call are called the arguments or actual parameters. The parameter used in function definition statement which contain data type on its time of declaration is called formal parameter.


2 Answers

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

public Player(char initIcon, int initX, int initY) {
    super(initIcon, initX, initY);
}
like image 161
Nikolay Kuznetsov Avatar answered Sep 28 '22 03:09

Nikolay Kuznetsov


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);
    }
...
like image 45
PermGenError Avatar answered Sep 28 '22 03:09

PermGenError