Search⌘ K
AI Features

Solution: Flexible Pizza Orders

Explore how to implement flexible pizza orders by using constructor chaining to manage default values such as crust and cheese. Understand how to encapsulate data and organize code efficiently with constructors in Java classes.

We'll cover the following...
Java 25
public class Pizza {
private String size;
private String crust;
private String cheese;
// Master Constructor: The only one that sets the fields
public Pizza(String size, String crust, String cheese) {
this.size = size;
this.crust = crust;
this.cheese = cheese;
}
// Intermediate: Supplies "Normal" cheese and delegates
public Pizza(String size, String crust) {
this(size, crust, "Normal");
}
// Basic: Supplies "Thin" crust and delegates to the Intermediate constructor
public Pizza(String size) {
this(size, "Thin");
}
public void printDescription() {
// Use standard concatenation to construct the output
System.out.println("Pizza: " + size + ", " + crust + " crust, " + cheese + " cheese.");
}
}
...