简体   繁体   中英

Are Java constructors called upon creating a class? “constructor cannot be applied to given types” error when no object has been created

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.

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:

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);
    }

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM