Search code examples
javainheritanceconstructorhierarchysuperclass

Undefined Constructors in Hierarchy


I am making a superclass Food with two subclasses: Dinner and Breakfast. Dinner and Breakfast will each have two of their own subclasses, Dinner will have Seafood and ChickenDish while Breakfast will have Cereal and Omelette. Right now I have only programmed Food and Dinner, but when I try to program in main, it says my constructors are undefined.

Here is my Food superclass:

public class Food {
private double price;
private int calories;
private boolean vegan;

public Food(double price, int calories, boolean vegan){
    this.price = price;
    this.calories = calories;
    this.vegan = vegan;
}

public double getPrice(){
    return price;
}

public int getCalories(){
    return calories;
}

public boolean getVegan(){
    return vegan;
}

}

and here is my Dinner subclass:

public class Dinner extends Food{

public Dinner(double price, int calories, boolean vegan){
    super (price, calories, vegan);
}

}

To keep this question open-ended, I'm just looking for a more conceptual answer so it can be applicable to other people's questions. How does one create a constructor for a super/subclass and how do I use it in main? Thanks all.

P.S.: I know I could just remove the "public Food" constructor and write "Food x = new Food;" in main but my comp sci teacher requests that you have a constructor in every class: super or sub.

EDIT: My main method is currently:

public class Main {
public static void main(String[] args){
    Food x = new Food();

}

}

to which I get "The constructor Food() is undefined". I know that I'm probably missing something/doing it wrong so I'm just looking for guidance in how to actually complete this task.


Solution

  • There are more than one type of constructors.

    One type is default constructor

    public Food(){
    
    }
    

    It is called whenever an object of your class is created. In this case, this constructor will be called from main if you create an object of your Food class like

    Food obj = new Food();
    

    The one you have defined in Food class is called parameterized constructor

    public Food(double price, int calories, boolean vegan){
        this.price = price;
        this.calories = calories;
        this.vegan = vegan;
    }
    

    This constructor will be called when you create an object of your Food class like

    Food obj = new Food(20.22, 10, true);
    

    You need to pass the values as arguments in order to call this constructor.

    Note that which constructor will be called depends on whether you pass argument values in constructor call or not.

    You are getting an error because in your main method

    public class Main {
    public static void main(String[] args){
        Food x = new Food();
    }
    

    you are calling the default constructor of your Food class but in your Food class, you have defined a parameterized constructor which expects 3 arguments.

    You need to pass those arguments when creating an object of class Food For example

    Food obj = new Food(20.22, 10, true);