class Salad{ private int numIngridients; public Salad(int numIngridients){ this.numIngridients = numIngridients; } public String[][] salatik(){ String[][] salaticOK = new String[numIngridients][3]; return salaticOK; } } class Calories extends Salad { } 

Error: java: Salad in class constructor Salad cannot be applied to given types; required: int found: no length

When pointing to the second class, in which I want to inherit the first class, it writes "There is no default constructor available in 'Salad'"

How can I inherit a class in this case?

    3 answers 3

    Error message

    Error: java: Salad in class constructor Salad cannot be applied to given types; required: int found: no length

    says that you have no default constructor in the class from which you are trying to inherit

    The default constructor is a constructor with no arguments, it is meant for classes that have no constructors in the listing and classes with the constructor with arguments already declared are not

     public Salad(){} 

    This is how the class should look like:

     class Salad { private int numIngridients; public Salad(){} public Salad(int numIngridients){ this.numIngridients = numIngridients; } public String[][] salatik(){ String[][] salaticOK = new String[numIngridients][3]; return salaticOK; } } 

    Inheritance is also possible if you explicitly inherit by specifying one of the existing constructors, which will be called when constructing an inheritor:

     class Calories extends Salad { public Calories (int numIngridients){ super(numIngridients); } } 

      There is no default constructor available in 'Salad' .

      There is no default constructor in the Salad class, so create it.

      • Please tell me how to do it - Vlad Black
      • and perhaps public Salad (int numIngridients) {this.numIngridients = numIngridients; is not a constructor? - Vlad Black
      • This is easy to do (in principle, it’s already written above) public Salad(){} . Yes, this is a constructor with a parameter, but an inherited default class must be created with exactly the same constructor with a parameter, but for current realities, an inherited class requires an object to be created by a constructor without parameters (default constructor), and since it does not exist, an error is generated . - keekkenen

      There is no default constructor available in 'Salad' "

      If there is no default constructor in the superclass, but in the subclass, then you can create a constructor with no arguments in the superclass (if this cannot be done, then see below). Without this, it is impossible to create an instance of a subclass, since the default constructor is a constructor with no arguments (the reverse is not true) and it will implicitly call the same superclass constructor when creating an instance of the subclass. And if there is any constructor in the subclass, then the default constructor does not exist.

      To create an instance of a subclass, it is required to sequentially call the constructors of the superclasses, so if any of the constructors is missing, then it must be created explicitly. If there is a constructor with arguments in the superclass, then there is no default constructor, and in the subclass you need to create the same constructor that the superclass constructor calls and passes arguments to it. To do this, in the first line of the subclass constructor, call the superclass constructor as follows

       super(*args*); 

      Something like that in this answer.

      It does not matter which constructor and how many arguments it has, the main thing is to call the superclass constructor when creating the subclass.

      If there is a constructor with no arguments in the subclass, it will be called implicitly from the default constructor, but in this case there is no such constructor, and for this reason an error occurs.