S06L08 – Inheritance 01- Understanding inheritance in programming

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:


5. Java Program Example for Inheritance


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


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.

Share your love