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!
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.
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.
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.
You need to initialize super class by call its constructor with super
public Player(char initIcon, int initX, int initY) {
super(initIcon, initX, initY);
}
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);
}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With