Java ProgramsOOPSingle Inheritance

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. 1class Car extends Vehicle establishes the one-parent, one-child relationship that defines single inheritance.
  2. 2brand is declared only in Vehicle, but Car can read it directly, since it's inherited.
  3. 3start() is declared only in Vehicle too, yet calling it on a Car object works without Car writing any code for it.
  4. 4honk() is Car's own addition, callable alongside everything inherited from Vehicle.
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

Related Programs