Parent class method is taking in a parameter (Noodle noodle) what does this mean?

Here is the code:

class Dinner {

  

  private void makeNoodles(**Noodle noodle**, String sauce) {

    

    noodle.cook();

    

    System.out.println("Mixing " + noodle.texture + " noodles made from " + noodle.ingredients + " with " + sauce + ".");

    System.out.println("Dinner is served!");

    

  }

  

  public static void main(String[] args) {

    

    Dinner noodlesDinner = new Dinner();

    // Add your code here:

    Noodle biangBiang = new BiangBiang();

    noodlesDinner.makeNoodles(biangBiang, "soy sauce and chili oil");

        

  }

  

}

Is the makeNoodles method taking in a parameter from the child class Noodle? Or are we initializing a new variable “noodle”? There is no String noodle; instance field declared in the Noodle class. But there is a Noodle() constructor:

class Noodle {

  

  protected double lengthInCentimeters;

  protected double widthInCentimeters;

  protected String shape;

  protected String ingredients;

  protected String texture = "brittle";

  

  Noodle(double lenInCent, double wthInCent, String shp, String ingr) {

    

    this.lengthInCentimeters = lenInCent;

    this.widthInCentimeters = wthInCent;

    this.shape = shp;

    this.ingredients = ingr;

    

  }

I was under the impression that each constructor parameter has to be preceded with either String, double, int, boolean, etc.

Edit: I just noticed that this method has a private aspect modifier as well, so it definitely can’t take any parameters or instance fields from another class, right?

Hello @method9130742469!

In this case, the parameter Noodle noodle means that the makeNoodles() method takes in an object, noodle, that is of the Noodle type. Noodle is a class with a constructor, and an instance of the Noodle class should be passed into makeNoodles(). Just as you could have a parameter of the int or String data types, you can also have a parameter for an instance of a class that you made.


The private access modifier simply means that makeNoodles() cannot be called outside of the Noodle class. See more on access modifiers here.

1 Like