Single Inheritance in Java
beginner· OOP · Inheritance
Problem
Single inheritance is the simplest inheritance relationship — exactly one subclass extends exactly one parent class, gaining direct access to everything the parent declares.
Create a Vehicle class and a Car subclass that inherits and uses Vehicle's field and method directly.
Input
new Car() calling start() and honk()
Output
Generic vehicle starting
Generic car honking
Java Program
Java
class Vehicle {
String brand = "Generic";
void start() {
System.out.println(brand + " vehicle starting");
}
}
class Car extends Vehicle {
void honk() {
System.out.println(brand + " car honking");
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Car car = new Car();
car.start(); // inherited directly from Vehicle
car.honk(); // defined in Car itself
}
}Output
Generic vehicle starting
Generic car honking
Core Logic
Extending a single parent class gives the subclass direct access to that parent's field and method, without redeclaring either of them.
How It Works
- 1
class Car extends Vehicleestablishes the one-parent, one-child relationship that defines single inheritance. - 2
brandis declared only inVehicle, butCarcan read it directly, since it's inherited. - 3
start()is declared only inVehicletoo, yet calling it on aCarobject works withoutCarwriting any code for it. - 4
honk()isCar's own addition, callable alongside everything inherited fromVehicle.
A
Car object can call both start(), inherited from Vehicle, and honk(), defined in Car itself — both use the same inherited brand field.💡
Key Point: Nothing here is overridden — Car simply reuses Vehicle's field and method as-is, which is the most basic thing inheritance provides before overriding or extending behavior enters the picture.
Key Concepts
extendsinherited fieldinherited method