Understanding Types of Inheritance in Using a Parking Lot System

Object-Oriented Programming (OOP) is a powerful paradigm, and inheritance is one of its core pillars. Python supports different types of inheritance, allowing developers to build hierarchical class relationships that mimic real-world systems.

In this blog post, we’ll use a Parking Lot System as our context to explore the following types of inheritance:

  • Single Inheritance

  • Multiple Inheritance

  • Multilevel Inheritance

  • Hierarchical Inheritance

  • Hybrid Inheritance

Let’s dive in!


🧱 1. Single Inheritance

One child class inherits from one parent class.

🚘 Example:

pythonCopyEditclass ParkingLot:
    def __init__(self, location):
        self.location = location

    def get_location(self):
        return f"Parking lot is at {self.location}"

class CarParking(ParkingLot):  # Single inheritance
    def park_car(self):
        return "Car parked successfully"

lot = CarParking("Downtown")
print(lot.get_location())
print(lot.park_car())

✅ Use Case:

You have a general parking lot class and want to extend it specifically for car parking, reusing shared functionality.


🧱 2. Multiple Inheritance

One child class inherits from more than one parent class.

🚦 Example:

pythonCopyEditclass CameraSystem:
    def monitor(self):
        return "Monitoring through CCTV"

class PaymentSystem:
    def process_payment(self):
        return "Payment processed successfully"

class SmartParking(CameraSystem, PaymentSystem):  # Multiple inheritance
    def open_gate(self):
        return "Gate opened"

sp = SmartParking()
print(sp.monitor())
print(sp.process_payment())
print(sp.open_gate())

✅ Use Case:

You want to build a SmartParking system that combines security features and payment management.


🧱 3. Multilevel Inheritance

A class inherits from a child class, which itself inherits from another parent class.

🏢 Example:

pythonCopyEditclass ParkingLot:
    def entry(self):
        return "Vehicle entered"

class CarParking(ParkingLot):
    def park_car(self):
        return "Car is parked"

class ElectricCarParking(CarParking):  # Multilevel inheritance
    def charge_car(self):
        return "Car is charging"

ecp = ElectricCarParking()
print(ecp.entry())
print(ecp.park_car())
print(ecp.charge_car())

✅ Use Case:

You first define a general parking system, then specialize it for cars, and further extend it for electric car charging stations.


🧱 4. Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

🚙 Example:

pythonCopyEditclass ParkingLot:
    def entry(self):
        return "Vehicle entered the parking lot"

class BikeParking(ParkingLot):
    def park_bike(self):
        return "Bike parked"

class TruckParking(ParkingLot):
    def park_truck(self):
        return "Truck parked"

bp = BikeParking()
tp = TruckParking()

print(bp.entry(), "|", bp.park_bike())
print(tp.entry(), "|", tp.park_truck())

✅ Use Case:

All types of vehicles (bikes, trucks, etc.) share common behavior but have their own specific features.


🧱 5. Hybrid Inheritance

A combination of more than one type of inheritance.

💡 Example:

pythonCopyEditclass BaseSystem:
    def log_event(self):
        return "Event logged"

class ParkingLot(BaseSystem):
    def entry(self):
        return "Vehicle entered"

class PaymentSystem(BaseSystem):
    def process_payment(self):
        return "Payment processed"

class SmartParking(ParkingLot, PaymentSystem):  # Hybrid inheritance
    def open_gate(self):
        return "Gate opened"

sp = SmartParking()
print(sp.log_event())
print(sp.entry())
print(sp.process_payment())
print(sp.open_gate())

✅ Use Case:

You want a SmartParking class to inherit logging from a common base class, and also inherit functionalities from both ParkingLot and PaymentSystem.


🧠 Key Takeaways

Inheritance TypeDescriptionUse in Parking Lot System
Single InheritanceOne child from one parentCarParking from ParkingLot
Multiple InheritanceOne child from multiple parentsSmartParking from CameraSystem + PaymentSystem
Multilevel InheritanceChain of inheritance across levelsElectricCarParking > CarParking > ParkingLot
Hierarchical InheritanceMultiple children from a single parentBikeParking, TruckParking from ParkingLot
Hybrid InheritanceMixture of multiple and hierarchicalSmartParking from ParkingLot & PaymentSystem (both from BaseSystem)

🏁 Final Words

Inheritance is a powerful tool in Python that promotes code reuse, scalability, and maintainability. The Parking Lot System is a perfect real-world analogy to understand different types of inheritance and how to model your systems using them effectively.

0
Subscribe to my newsletter

Read articles from Nilkanta Dasadhikari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanta Dasadhikari
Nilkanta Dasadhikari

Tech lead exploring Generative AI & ML 🔍 | Python, JS, React | Aviation meets Innovation ✈ | Writing what I learn. Learning what I build.