1

So I am currently working through bug testing some of my code, and I seem to be getting this error:

constructor Being in class Being cannot be applied to given types;
public class Monster extends Being{
   ^
required: String,String
  found: no arguments
  reason: actual and formal argument lists differ in length

Here is a sample from my code: (Parent)

public abstract class Being{
public String name;
public Race race;
public int str = 10, agi = 10, con = 10, wis = 10, HP, MP, tHP, tMP, lvl;

//Object Creator

public Being(String n, String rce){
    name = n;
    tHP = (con/2) + 10;
    tMP = (wis/2) + 10;
    HP = tHP;
    MP = tMP;
    lvl = 1;
    }
}

(Child)

public class Monster extends Being{

private Monster m = new Monster();
}

This is just a small example. I've tried many different ways of creating the constructor in Monster.java, but every time I come out with the same error. Someone please help!

tanishalfelven
  • 1,186
  • 2
  • 10
  • 21
  • That's how java works, with regards to constructors/polymorphism – vandale Jan 15 '14 at 02:23
  • That's what I was afraid of... any way I might be able to get around it? – tanishalfelven Jan 15 '14 at 02:25
  • when instantiating a sub class, java will first instantiate all its base class, and will instantiate them with the signature similar to your sub class in default. in your code, your base class have no default constructor, hence java can not do it. – Rugal Jan 15 '14 at 02:28

3 Answers3

1

Every constructor must call a constructor in the super class. If you don't call one, the default constructor (the no-args one) will be called implicitly.

In your case, there is no no-args constructor in the super class, so you must call the String, String one explicitly, eg:

public class Monster extends Being {
    public Monster() {
        super("foo", "bar");
    }
}

or declare a no-args constructor in Being.

The weird thing you should remember about java is, if you don't specify any constructors, a no-args one is implicitly defined, but when you define a constructor with args, the implicit no-args constructor disappears! This catches out most new java programmers.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • So if in Being.java I added a public Being() method, then I tried this it should work? EDIT: Tried it out, and it works, thanks! – tanishalfelven Jan 15 '14 at 02:28
  • Yes. The weird thing is, if you don't specify any constructors, a no-args one is implicitly defined, but when you define a constructor with args, the implicit no-args constructor disappears! This catches out most new java programmers (I got caught out too!) – Bohemian Jan 15 '14 at 02:30
  • Yeah it is all pretty wonky... I've played with this stuff before and been fine, so I was kind of freaking out... but either way thank you! – tanishalfelven Jan 15 '14 at 02:31
1

First, the no-arg constructor is assumed by the compiler. That said, the main thing to remember about Java constructors is that once a constructor other than the no-arg constructor is defined, any subclasses must call it.

So, since your parent class defined a constructor that takes two strings, your child class must invoke it. Your options are:

  1. Create a two-string constructor in the child class
  2. Create some other constructor in the child class, but must invoke the two-string constructor in the parent,
  3. Create a no-arg constructor in the parent class.

(There are probably more exotic options, but those are the top three, I think)

In code:

1:

public class Monster extends Being {
  . . .
  public Monster(String n, String rce) {
    super(n, rce);
  }
  . . .
}

2:

public class Monster extends Being {
  . . .
  public Monster() {
    super("Default n", "Default rce");
    // or
    // super(null, null);
    // or
    // super("Default n", null);
    // etc
  }
  . . .
}

3:

public abstract class Being {
  . . .
  public Being() {
    . . .
  }
  public Being(String n, String rce) {
   . . .
  }
  . . .
}
. . .
public class Monster extends Being {
. . .
  public Monster() {
    super();
  }
. . .
}

The point being that once you declare a constructor, subclasses must invoke it. I suppose you could remove the two-string constructor from your Being class, but that's really a design decision.

HTH

J Steven Perry
  • 1,711
  • 1
  • 17
  • 28
0
  1. explicitly call the Being constructor (via super("aname","aRace");)
  2. create a parameterless constructor in Being

do note that if you choose the second option, name and race will be null

vandale
  • 3,600
  • 3
  • 22
  • 39