Inheritance

Inheritance allows a class to acquire properties and methods from another class.

class Vehicle {
    protected String brand = "Toyota";

    public void honk() {
        System.out.println("Beep beep!");
    }
}

class Car extends Vehicle {
    private String model;

    public Car(String model) {
        this.model = model;
    }

    public void displayInfo() {
        System.out.println(brand + " " + model);
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Corolla");
        myCar.honk(); // Output: Beep beep!
        myCar.displayInfo(); // Output: Toyota Corolla
    }
}

Inheritance — Structure map

Clickable & Draggable!

Inheritance — Related pages: