Single & Multilevel Inherit
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 25 of 87.
So far, we've seen classes extending one parent class directly. But inheritance can form different structures depending on how classes relate to each other. Let's explore the two main patterns: single inheritance and multilevel inheritance.
Single inheritance is what we've been using. A class extends exactly one parent class directly:
public class Animal { }
public class Dog extends Animal { }
public class Cat extends Animal { }Here, both Dog and Cat inherit from Animal, but they don't inherit from each other. Each child has one direct parent.
Multilevel inheritance creates a chain where a class extends another class, which itself extends another class:
public class Animal {
public void eat() {
System.out.println("Eating");
}
}
public class Mammal extends Animal {
public void breathe() {
System.out.println("Breathing");
}
}
public class Dog extends Mammal {
public void bark() {
System.out.println("Barking");
}
}In this chain, Dog inherits from Mammal, which inherits from Animal. A Dog object has access to all methods from the entire chain:
Dog dog = new Dog();
dog.eat(); // From Animal
dog.breathe(); // From Mammal
dog.bark(); // From DogRemember that Object sits at the top of every chain. So our Dog actually has four levels: Object -> Animal -> Mammal -> Dog. Constructor chaining flows through all these levels when creating a new object.
Challenge
EasyLet's build a transportation hierarchy that demonstrates both single and multilevel inheritance patterns. You'll create a chain of classes where each level adds new capabilities, showing how methods are inherited through multiple levels.
You'll organize your code across four files:
Vehicle.java: Create the base class at the top of your hierarchy. Every vehicle should have:- A private field for
brand(String) - A constructor that accepts the brand
- A method
getBrand()that returns the brand - A method
move()that prints:[brand] is moving
- A private field for
LandVehicle.java: Create a class that extends Vehicle, representing the middle level of your multilevel chain. Land vehicles add ground-specific behavior:- A private field for
wheels(int) - A constructor that takes brand and wheels, using
super(brand)for the parent portion - A method
getWheels()that returns the wheel count - A method
honk()that prints:[brand] honks!
- A private field for
Car.java: Create a class that extends LandVehicle, completing your three-level inheritance chain. Cars add their own specialized features:- A private field for
model(String) - A constructor that takes brand, wheels, and model, using
super(brand, wheels) - A method
getModel()that returns the model - A method
displayInfo()that prints:[brand] [model] with [wheels] wheels
- A private field for
Main.java: Demonstrate how a Car object has access to methods from all three levels of the hierarchy. You'll receive three inputs: brand (String), wheels (int), and model (String). Create a Car and call these methods in order:move()- inherited from Vehicle (grandparent)honk()- inherited from LandVehicle (parent)displayInfo()- defined in Car itself
You will receive three inputs: the brand name, number of wheels, and model name.
Your output should show three lines demonstrating how the Car object can use methods from every level of its inheritance chain - from the top-level Vehicle all the way down to its own class!
Cheat sheet
Java supports different inheritance structures. The two main patterns are single inheritance and multilevel inheritance.
Single inheritance occurs when a class extends exactly one parent class directly:
public class Animal { }
public class Dog extends Animal { }
public class Cat extends Animal { }Both Dog and Cat inherit from Animal, but not from each other.
Multilevel inheritance creates a chain where a class extends another class, which itself extends another class:
public class Animal {
public void eat() {
System.out.println("Eating");
}
}
public class Mammal extends Animal {
public void breathe() {
System.out.println("Breathing");
}
}
public class Dog extends Mammal {
public void bark() {
System.out.println("Barking");
}
}In multilevel inheritance, a child class has access to all methods from the entire inheritance chain:
Dog dog = new Dog();
dog.eat(); // From Animal
dog.breathe(); // From Mammal
dog.bark(); // From DogEvery class hierarchy has Object at the top. Constructor chaining flows through all levels when creating a new object.
Try it yourself
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read inputs
String brand = scanner.nextLine();
int wheels = scanner.nextInt();
scanner.nextLine(); // consume newline
String model = scanner.nextLine();
// TODO: Create a Car object with the input values
// TODO: Call move() - inherited from Vehicle (grandparent)
// TODO: Call honk() - inherited from LandVehicle (parent)
// TODO: Call displayInfo() - defined in Car itself
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System