0

I'm reconstructing the following code from a tutorial. When I run complie it I get the following error:

constructor Vierbeiner in class Vierbeiner cannot be applied to given types; required: java.lang.String found: no arguments reason: actual and formal argument lists differ in length

My understanding is that this error occurs because the parent class Vierbeiner has a constructor that requires a String argument and no constructor requiring no argument. What I don't understand is why I get this error without creating an object of class Hund. Why is a constructor being called when I haven't created an object of class Hund? Every existing question I've seen about this error involves creating an object of the child class.

public class Vierbeiner {

    public static void main(String[] args){
         Vierbeiner hund = new Vierbeiner("Bello");
         hund.rennen();
    }

    String vierbeinerName;

    Vierbeiner(String pName) {
        vierbeinerName = pName;
    }

    public void rennen() {
        System.out.println(vierbeinerName+" rennt los!");
    }

}

class Hund extends Vierbeiner {
}

EDIT: In reponse to the answers I've gotten, what's still not clear to me is why the following (case 1) compiles with no problem:

public class Vierbeiner{
    public static void main(String[] args){

         Vierbeiner hund = new Vierbeiner();
         Hund hund2 = new Hund();  
         // Hund hund3 = new Hund("doggy");    
    }

   String vierbeinerName;

   Vierbeiner() {
       vierbeinerName = "test";
   };

   Vierbeiner(String pName){

   }

   }

class Hund extends Vierbeiner{

};

In which I create a Hund object seemingly using the no argument constructor which I defined in the Vierbeiner class.

But if I uncomment the following (case 2) I get a compilation error:

Hund hund3 = new Hund("doggy");

In case 1, the compiler looks to the parent class Vierbeiner to find a no argument constructor to create the hund2 object. In case 2 I would expect the compiler to do the same thing, that is go to the parent class Vierbeiner to find a string argument constructor to create the hund3 object, but this doesn't seem to be the case without using "super" and I don't understand why.

Ronald
  • 343
  • 3
  • 10
  • @ErwinBolwidt I misspoke in saying it occurred when I "run" the program as it's a compilation error but that is the error that I get from the code that I posted. – Ronald Jun 10 '18 at 17:12
  • 1
    Your 1st case runs since you added a non-parameterized constructor to your base class, which your Hund class uses implicitly via a default constructor. Your 2nd case fails since the Hund class does not have an explicit constructor with a single string parameter. The default constructor only applies for a non-parameterized invocation. – Rann Lifshitz Jun 10 '18 at 17:50
  • 1
    @RannLifshitz Thank you, I didn't understand that a default constructor for Hund was being created even though the constructor for Vierbeiner is the one actually used to create the Hund object. So if I understand correctly: In both cases, a default constructor for Hund is created. Because Hund extends Vierbeiner, the Hund default constructor checks for a workable constructor in the parent class. In case 1 it finds a workable constructor in the parent class, in case 2 it does not. – Ronald Jun 10 '18 at 22:26
  • Happy to Help, buddy – Rann Lifshitz Jun 10 '18 at 23:54

2 Answers2

5

You are receiving an error during compilation, not an error or exception during runtime.

This means that the code is not being executed, so no class instances have been created - there is no invocation of any constructor.

During compilation, it is easy to detect that you are extending a class which has a single string argument constructor, which the extending class must call (in order to create the underlying base class).

Note that once you have implemented a constructor, you can no longer rely upon the autogenerated no-parameter default constructor of your base class - you must now invoke the parameterized constructor of your base class in your deriving class.

Since this sort of implementation is lacking in your code - the error you described occurs.

A simple possible solution would be to provide a default constructor implementation which will invoke the single parameter constructor of the base class:

class Hund extends Vierbeiner{
    Hund() {
        super("DefaultName");
    }    

};

This addition to your code solves the compilation time error.

A more practical approach would be to make sure that your deriving class has a constructor with a string parameter as well, which can then be passed to the base class constructor:

class Hund extends Vierbeiner{
    Hund(String name) {
        super(name);
    }    

};

References:

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • Thank you for your answer. "During compilation, it is easy to detect that you are extending a class which has a single string argument constructor, which the extending class must call" - I guess what I still don't understand is why that is considered an error and why it prevents the code from running. Why can't I just create Hund objects with a string argument (e.g Hund dog = new Hund("Max") constructor from Vierbeiner without using "super"? – Ronald Jun 10 '18 at 17:05
  • What I would expect to happen is: 1. The compiler looks for a string argument constructor in the Hund class, doesn't find one. 2. The complier looks for a string argument in the parent class Hund is extended from, it finds one and uses it to create the Hund object. I don't understand why that doesn't happen without "super". Were I to define a no argument constructor in the Vierbeiner class that is what would happen(afaik) but as soon as we add a String argument that doesn't work anymore. I edited the question to better explain. – Ronald Jun 10 '18 at 17:30
  • 1
    @Ronald Your Hund class has no constructor implementation. This means that the default constructor is used - a constructor without any parameters which sets default values to the class attributes (and in this case) and invokes a non parameterized base class constructor, if it exists. If it does not exists - then you will get a compilation error. Your expectations are not how Java works. `super` is used during the implementation of deriving class constructor in order to call the base class constructors. – Rann Lifshitz Jun 10 '18 at 17:47
2

You have a compile time error. You need to add the constructor in your derived class calling the base clase constructor using the super keyword.

class Hund extends Vierbeiner {

    public Hund(String pName) {
        super(pName);
    }

};
Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44