Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S06L08 - Inheritance 01- Understanding inheritance in programming

Java Articles
1 views

S06L08 - Inheritance 01- Understanding inheritance in programming


February 13, 2025

Understanding Inheritance in Object-Oriented Programming: A Practical Guide Using Vehicle Classes in Java


Table of Contents


  1. Introduction ……………………………………………………….. Page 2
  2. Understanding Inheritance ……………………………………… Page 3
    1. The Concept of a Parent Class ………………………… Page 3
    2. Child Classes and Their Unique Properties ……… Page 4
  3. Comparing Vehicle Types: Bike, Car, and Truck …………… Page 5
  4. Diagrammatic Representation of Inheritance ………………… Page 6
  5. Java Program Example ………………………………………….. Page 7
  6. Step-by-Step Code Explanation ……………………………… Page 8
  7. Conclusion ………………………………………………………… Page 9

1. Introduction


Inheritance is a powerful concept in object-oriented programming (OOP), enabling programmers to create a new class based on an existing class. In this eBook, we use a practical example from the transportation domain by discussing how different types of vehicles — bikes, cars, and trucks — share common properties through inheritance. This guide is crafted for beginners and developers with basic knowledge in Java. Here, you will find an overview of inheritance, key differences among vehicle types, a clear diagrammatic representation, and a complete Java sample code demonstrating how to implement inheritance.

Key Points Covered:
  • An understanding of how inheritance reduces code duplication.
  • How a parent class (Vehicle) can hold common properties.
  • How child classes (Bike, Car, and Truck) include both shared and unique properties.
  • Practical application in Java with sample code and detailed syntax explanations.
Pros and Cons of Using Inheritance:
  • Pros: Streamlines code, promotes reusability, simplifies maintenance.
  • Cons: Can lead to tight coupling and over-complex hierarchies if misused.

Below is a comparative overview of two main approaches:

Comparison Table: Direct Property Initialization vs. Inheritance
Approach Direct Initialization Inheritance
Code Duplication High Low
Maintenance Complexity Higher Lower
Scalability Limited More scalable

The table above highlights how inheritance allows developers to reduce duplicated code when many classes share common properties.


2. Understanding Inheritance


Inheritance is a core principle in OOP where a new class, called a child class, inherits attributes and methods from an existing class, known as the parent class.


2.1 The Concept of a Parent Class


A parent class acts as the foundation for common properties. In our example, the Vehicle class contains attributes common across all vehicles such as:

  • Engine
  • Wheels
  • Seats
  • Fuel tank
  • Lights

By encapsulating these universal properties in one class, child classes can easily inherit them without re-initializing for every new vehicle.


2.2 Child Classes and Their Unique Properties


Child classes like Bike, Car, and Truck inherit from the Vehicle class and add their unique properties:

  • Bike: Includes a handle.
  • Car: Adds steering, music system, seat belt, air conditioner, fridge, entertainment system.
  • Truck: Similar to cars but replaces the entertainment system/fridge with a container (or sometimes includes both in complex implementations).

3. Comparing Vehicle Types: Bike, Car, and Truck


The following table illustrates the distinct properties of each vehicle type compared to the shared properties in the Vehicle class.

Vehicle Properties Comparison Table
Property Vehicle: Common Properties
Engine Present in Bike, Car, and Truck
Wheels Present in Bike, Car, and Truck
Seats Present in Bike, Car, and Truck
Fuel Tank Present in Bike, Car, and Truck
Lights Present in Bike, Car, and Truck

Unique Additions:

Vehicle Unique (Child) Properties
Bike Handle
Car Steering, Music system, Seat belt, Air conditioner, Fridge, Entertainment system
Truck Steering, Music system, Seat belt, Air conditioner, Container

This structured property distribution ensures that all vehicle types maintain a basic structure while allowing specific customizations.


4. Diagrammatic Representation of Inheritance


Below is a simplified block diagram illustrating the relationship between the Vehicle class and its child classes:

         [Vehicle]
         ┌─────────────────────┐
         │ Engine              │
         │ Wheels              │
         │ Seats               │
         │ Fuel Tank           │
         │ Lights              │
         └─────────────────────┘
               /    |    \
              /     |     \
   [Bike]   [Car]  [Truck]
      |        |        |
      |        |        |
   Handle   Steering &   Steering &
           additional    additional          
           entertainment  features (container)

5. Java Program Example for Inheritance


Below is a sample Java code snippet that demonstrates how inheritance works in a vehicle-based application:


public class Vehicle {
    String engine;
    int wheels;
    int seats;
    String fuelTank;
    boolean lights;

    // Constructor for Vehicle
    public Vehicle(String engine, int wheels, int seats, String fuelTank, boolean lights) {
        this.engine = engine;
        this.wheels = wheels;
        this.seats = seats;
        this.fuelTank = fuelTank;
        this.lights = lights;
    }
    
    public void displaySpecs() {
        System.out.println("Engine: " + engine);
        System.out.println("Wheels: " + wheels);
        System.out.println("Seats: " + seats);
        System.out.println("Fuel Tank Capacity: " + fuelTank);
        System.out.println("Lights present: " + lights);
    }
}

public class Bike extends Vehicle {
    String handle;

    public Bike(String engine, int wheels, int seats, String fuelTank, boolean lights, String handle) {
        super(engine, wheels, seats, fuelTank, lights);
        this.handle = handle;
    }

    public void displayBikeSpecs() {
        displaySpecs();
        System.out.println("Handle: " + handle);
    }
}

public class Car extends Vehicle {
    String steering;
    String musicSystem;
    String seatBelt;
    String airConditioner;
    String fridge;
    String entertainmentSystem;

    public Car(String engine, int wheels, int seats, String fuelTank, boolean lights,
               String steering, String musicSystem, String seatBelt,
               String airConditioner, String fridge, String entertainmentSystem) {
        super(engine, wheels, seats, fuelTank, lights);
        this.steering = steering;
        this.musicSystem = musicSystem;
        this.seatBelt = seatBelt;
        this.airConditioner = airConditioner;
        this.fridge = fridge;
        this.entertainmentSystem = entertainmentSystem;
    }

    public void displayCarSpecs() {
        displaySpecs();
        System.out.println("Steering: " + steering);
        System.out.println("Music System: " + musicSystem);
        System.out.println("Seat Belt: " + seatBelt);
        System.out.println("Air Conditioner: " + airConditioner);
        System.out.println("Fridge: " + fridge);
        System.out.println("Entertainment System: " + entertainmentSystem);
    }
}

public class Truck extends Vehicle {
    String steering;
    String musicSystem;
    String seatBelt;
    String airConditioner;
    String container;

    public Truck(String engine, int wheels, int seats, String fuelTank, boolean lights,
                 String steering, String musicSystem, String seatBelt, String airConditioner, String container) {
        super(engine, wheels, seats, fuelTank, lights);
        this.steering = steering;
        this.musicSystem = musicSystem;
        this.seatBelt = seatBelt;
        this.airConditioner = airConditioner;
        this.container = container;
    }

    public void displayTruckSpecs() {
        displaySpecs();
        System.out.println("Steering: " + steering);
        System.out.println("Music System: " + musicSystem);
        System.out.println("Seat Belt: " + seatBelt);
        System.out.println("Air Conditioner: " + airConditioner);
        System.out.println("Container: " + container);
    }
}

public class Main {
    public static void main(String[] args) {
        Bike bike = new Bike("500cc Engine", 2, 1, "10L", true, "Drop Handle");
        Car car = new Car("2000cc Engine", 4, 5, "50L", true, "Power Steering",
                          "Premium Audio", "Standard", "Dual Zone", "Mini Fridge", "Touch Screen");
        Truck truck = new Truck("4000cc Engine", 6, 3, "150L", true, "Hydraulic Steering",
                                "Advanced Audio", "Heavy Duty", "Refrigerated", "20ft Container");

        System.out.println("----- Bike Specifications -----");
        bike.displayBikeSpecs();
        System.out.println("\n----- Car Specifications -----");
        car.displayCarSpecs();
        System.out.println("\n----- Truck Specifications -----");
        truck.displayTruckSpecs();
    }
}

6. Step-by-Step Code Explanation


The Vehicle Class:

  • Contains common properties: engine, wheels, seats, fuel tank, and lights.
  • Its constructor initializes these attributes.
  • The method displaySpecs() prints these common specifications.

The Bike Class:

  • Inherits from Vehicle using the super() method to initialize common attributes.
  • Adds the unique property “handle” and the method displayBikeSpecs() to show all specifications.

The Car and Truck Classes:

  • Extend Vehicle and include additional unique attributes.
  • Each has a corresponding display method (displayCarSpecs() and displayTruckSpecs()) that prints both inherited and unique properties.

Main Class:

  • Instantiates objects for Bike, Car, and Truck.
  • Calls the display methods to print out the specifications for each vehicle, demonstrating the effectiveness of inheritance in reducing code redundancy.

7. Conclusion


In this eBook, we explored the concept of inheritance in Java using a real-world example of vehicle classes. We learned how the Vehicle class acts as a parent class holding common properties, while the Bike, Car, and Truck classes inherit these properties and extend them with unique attributes. The provided code clearly illustrates how inheritance promotes reusability and streamlines object-oriented design, reducing code duplication and simplifying maintenance.

If you are beginning your journey into Java programming or want to strengthen your understanding of OOP fundamentals, mastering inheritance is essential. Use this guide as a reference point for implementing clean, scalable, and manageable code in your future projects.

Happy Coding!

Note: This article is AI generated.

Tags:
beginner programming
inheritance
inheritance diagram
Java code
Java inheritance
Java tutorial
object-oriented programming
OOP fundamentals
vehicle class
Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.