Joseph Erickson
Joseph Erickson

Reputation: 960

java - constructor cannot be applied to given types

I'm developing a simple game. I created a class called Monster with a constructor to act as a basic template for all monsters. Here is the Monster class:

public class Monster {
    public int attackPower;
    public String weaponName;
    public int armorLevel;
    public String armorName;
    public int life;
    public String monsterClass;
    public String monsterName;
    public int monsterNumber;

    public Monster(int attackPower, String weaponName, int armorLevel, String armorName, int life, String monsterClass, String monsterName, int monsterNumber) {
        this.attackPower = attackPower;
        this.weaponName = weaponName;
        this.armorLevel = armorLevel;
        this.armorName = armorName;
        this.life = life;
        this.monsterClass = monsterClass;
        this.monsterName = monsterName;
        this.monsterNumber = monsterNumber;
    }
}

Here is the class I put together to test the constructor:

public class Area1Monsters extends Monster {
    public static void main(String[] args) {

        Monster rat1 = new Monster(1, "Claws", 3, "Fur", 9, "Fields", "Rat", 1);

        System.out.println("Attack Power: " + rat1.attackPower);
        System.out.println("Weaon Name: " + rat1.weaponName);
        System.out.println("Armor Level: " + rat1.armorLevel);
        System.out.println("Armor Name: " + rat1.armorName);
        System.out.println("HP: " + rat1.life);
        System.out.println("Starting Area: " + rat1.monsterClass);
        System.out.println("Name: " + rat1.monsterName);
        System.out.println("Monster Number: " + rat1.monsterNumber);
    }
}

The error says constuctor Monster in class Monster cannot be applied to given type; required: int, java.lang.String, int, java.lang.String...etc.

I believe I have correctly matched each of the data types from the constructor to the application of the object creation of rat1, but I'm clearly missing something. I'm sure it's obvious and basic. Any help would be very much appreciated.

Upvotes: 0

Views: 1613

Answers (3)

Giulio Biagini
Giulio Biagini

Reputation: 920

You have to change your Area1Monsters declaration in this way:

public class Area1Monsters 

without the extends keyword.

Upvotes: 5

ferjani
ferjani

Reputation: 99

I believe this is related to the inheritance. Your subclass has an implicit empty constructor.

You need to define an empty constructor in your superclass or to define an explicit constructor in Area1Monsters. The explicit constructor must call the superclass constructor (with default or null values)

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

Given the lack of any explicit constructor in the subclass, the compiler generates a default constructor that expects to be able to call super(), the superclass constructor iwth no parameters. To subclass a class without a parameterless constructor, you need to supply an explicit constructor that calls the superclass constructor you have.

Alternatively, remove the "extends" clause from Area1Monsters.

Upvotes: 4

Related Questions