0

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.

3
  • 1
    Can you give the exact error instead of truncating with "etc"? Off the cuff I'd guess that if everything seems to match up, you've probably compiled one file but not recompiled the other. Commented Jan 13, 2015 at 18:04
  • Sure here's the exact error: Constuctor Monster in class Monster cannot be applied to given type; required: int, java.lang.String, int, java.lang.String, int, java.lang.STring, java.lang.String, int; I assume this correlates to the specific types in the constructor Commented Jan 13, 2015 at 18:06
  • 1
    Just edit it into your question please. Also, note that you're also going to have a problem based on the fact that you are extending Monster for your main class. It probably should not extend Monster, and if it should, you'll need to properly call the super constructor. Commented Jan 13, 2015 at 18:08

3 Answers 3

5

You have to change your Area1Monsters declaration in this way:

public class Area1Monsters 

without the extends keyword.

Sign up to request clarification or add additional context in comments.

6 Comments

Not sure why this is getting downvoted so heavily; it's probably the correct solution. In light of Patricia's answer just before it, it is however redundant.
Interestingly enough, this was the solution that worked before i added "extends", I was simply under the impression "extends" was necessary. Apparently not.
Indeed. The only reason why the default constructor is needed at all is because the test class inherits from the Monster class, which it shouldn't, because it does not represent any kind of Monster.
@JosephErickson - If you use the Area1Monsters class as the Main class (the class which only contains the main() function), no extends keyword is required.
Trivial edit so I can convert my downvote to an upvote
|
4

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.

6 Comments

Thanks Patricia. Apparently I've understood the idea of superclass and subclass incorrectly. I was under the impression that Monster would act as a superclass, and Area1Monster would act as a child or a subclass to the superclass Monster. Is this incorrect?
Superclass/subclass are used with "is-a" relation. So you might say Goblin extends Monster because a goblin is a Monster. If you can't express the relationship with "is-a", it probably shouldn't be a subclass.
Right...but accordingly, rat1 IS A monster. Why then should it not extend monster?
@JosephErickson: rat1 is a monster, but it is not an instance of Area1Monsters. The question is "is Area1Monsters a Monster?"
That's fine. The dubious part of your code is where Area1Monsters extends Monster. Arguably a collection of Monsters is not, itself, a Monster. If it is then, as Patricia says, you need to define the constructor for Area1Monsters.
|
0

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)

Comments

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.