Inheritance lets a subclass receive attributes and methods defined by a parent, then add or change behavior. It fits a stable “is-a” relationship: Food is a Product, and Drink is a Product. The parent holds genuinely shared features; subclasses describe differences and avoid duplication.
In everyday terms, a Vehicle may provide a brand and movement behavior; a Bicycle adds pedals and a Car adds an engine. Inheritance does not copy a parent object. It establishes a type relationship: Java looks for a method on the subclass first, then follows the inheritance chain to its parent.
Parent construction and subclass extension
Inheritance assembly line: parent features flow into subclasses
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
This lesson uses one class per file, with interfaces in separate files too. Match each filename to its type name. Keep files from the same example in one directory, and use separate directories for different examples to avoid duplicate class names.
Product.java
public class Product {
private final String name;
private final double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
String getName() { return name; }
double getPrice() { return price; }
}Food.java
public class Food extends Product {
private final int calories;
Food(String name, double price, int calories) {
super(name, price);
this.calories = calories;
}
}Drink.java
public class Drink extends Product {
private final String size;
Drink(String name, double price, String size) {
super(name, price);
this.size = size;
}
}KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Overriding and method lookup
A subclass overrides a method by defining the same method signature as its parent. For food.toString(), Java chooses Food's version first. That override may still call super.toString(), which is useful for keeping the common result and adding detail.
Product.java
public class Product {
private final String name;
private final double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return String.format("%s (%.2f)", name, price);
}
}Food.java
public class Food extends Product {
private final int calories;
Food(String name, double price, int calories) {
super(name, price);
this.calories = calories;
}
@Override
public String toString() {
return String.format("Food: %s, %d kcal", super.toString(), calories);
}
}Main.java
public class Main {
public static void main(String[] args) {
Food food = new Food("Cheeseburger", 5.45, 302);
System.out.println(food);
}
}Burger object kitchen: parent recipe plus subclass ingredients
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Make EBook inherit Product
- Product stores name and price.
- EBook(Product) adds file_size.
- Use super for shared fields.
- Print all three fields.
Override a notification method
- Notification.send() returns a generic message.
- EmailNotification and SMSNotification inherit it.
- Override send() with channel-specific output.
- Create and call one object of each type.
Extend the restaurant menu model
- Create Product(name, price) and toString.
- Food adds calories; Drink adds size.
- Both call super and override toString.
- Create at least two Food and two Drink objects and print each.