Extending Parent Classes Templates into Child Models

🏷️ Object-Oriented Programming (OOP) Basics / Inheritance

🎯 Context Introduction

In object-oriented programming, inheritance allows us to create new classes (child models) that reuse, extend, and modify the behavior of existing classes (parent templates). Think of it like a blueprint: you have a basic house design (parent class), and you create specialized versions like a house with a garage or a house with a sunroom (child classes). This approach saves time, reduces code duplication, and makes your code more organized and maintainable.


⚙️ What is Inheritance?

  • Parent Class (Base Class): The original template that contains common attributes and methods.
  • Child Class (Derived Class): A new class that inherits everything from the parent and can add or override features.
  • Key Benefit: Write once, reuse many times. Changes to the parent automatically reflect in all children.

🛠️ Creating a Parent Class Template

A parent class serves as the foundation. It defines the shared characteristics that all child classes will inherit.

  • Define a class with common attributes and methods.
  • Use the init method to initialize shared properties.
  • Example structure for a parent class:
  • Class Name: NetworkDevice
  • Attributes: hostname, ip_address, status
  • Methods: connect(), disconnect(), get_info()

📊 Extending with Child Models

Child classes inherit everything from the parent and can:

  • Add new attributes specific to the child.
  • Add new methods unique to the child.
  • Override parent methods to change behavior.
  • Call parent methods using the super() function.

Comparison: Parent vs Child Class

Feature Parent Class Child Class
Attributes Common ones only Inherited + new ones
Methods General purpose Inherited + specialized
Flexibility Fixed template Can be customized
Code Reuse Foundation Extends foundation

🕵️ How Child Models Inherit and Extend

When creating a child model:

  • The child class name is followed by the parent class name in parentheses.
  • The child's init method calls the parent's init using super() to initialize inherited attributes.
  • New attributes are added after the super() call.
  • New methods are defined just like in any class.
  • To override a method, define it with the same name in the child class.

Key points to remember: - A child class can access all non-private parent methods and attributes. - Multiple children can inherit from the same parent. - A child can become a parent for further inheritance (multi-level).


📈 Practical Example Breakdown

Consider a parent class called Server with: - Attributes: name, os, ram_gb - Methods: start(), stop(), restart()

Now create a child class called WebServer that extends Server: - Inherited attributes: name, os, ram_gb - New attributes: port, domain - Inherited methods: start(), stop(), restart() - New method: serve_page() - Overridden method: start() (to also check port availability)

Another child class called DatabaseServer that extends Server: - Inherited attributes: name, os, ram_gb - New attributes: db_type, max_connections - Inherited methods: start(), stop(), restart() - New method: run_query() - Overridden method: stop() (to flush data before shutdown)


🧩 Best Practices for Extending Parent Classes

  • Keep parent classes generic – They should represent common functionality.
  • Keep child classes specific – Add only what is unique to that model.
  • Use super() consistently – Always initialize parent attributes properly.
  • Avoid deep inheritance chains – More than 2-3 levels can become confusing.
  • Override methods with care – Ensure the overridden method still fulfills the parent's contract.
  • Document your extensions – Explain why a child class exists and what it adds.

✅ Summary

Extending parent class templates into child models is a powerful way to build scalable and maintainable code. By starting with a solid parent template and creating specialized children, you can model real-world relationships cleanly. Remember: parent classes provide the foundation, child classes add the details. This approach keeps your code DRY (Don't Repeat Yourself) and makes future changes easier to manage.


Extending parent classes templates into child models lets engineers create specialized versions of a base class that inherit its attributes and methods while adding new features.


🔧 Example 1: Basic Parent Class with a Simple Child

This example shows how a child class inherits a method from its parent class without any changes.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class Car(Vehicle):
    pass

my_car = Car()
result = my_car.move()

📤 Output: Vehicle is moving


🔧 Example 2: Child Class Adding Its Own Method

This example demonstrates a child class that inherits a parent method and adds a brand-new method of its own.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class Car(Vehicle):
    def honk(self):
        return "Beep beep!"

my_car = Car()
move_result = my_car.move()
honk_result = my_car.honk()

📤 Output: Vehicle is moving
📤 Output: Beep beep!


🔧 Example 3: Overriding a Parent Method in the Child

This example shows how a child class can replace a parent method with its own version.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class Bicycle(Vehicle):
    def move(self):
        return "Bicycle is pedaling"

my_bike = Bicycle()
result = my_bike.move()

📤 Output: Bicycle is pedaling


🔧 Example 4: Calling the Parent Method from Inside the Child

This example demonstrates how a child class can extend a parent method by calling the parent version first, then adding extra behavior.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class ElectricCar(Vehicle):
    def move(self):
        parent_move = super().move()
        return parent_move + " silently"

my_ev = ElectricCar()
result = my_ev.move()

📤 Output: Vehicle is moving silently


🔧 Example 5: Practical Engineering Example — Sensor Base Class

This example shows a practical pattern where engineers create a base sensor class and extend it for different sensor types.

class Sensor:
    def __init__(self, location):
        self.location = location

    def read(self):
        return 0

class TemperatureSensor(Sensor):
    def read(self):
        return 25.5

class PressureSensor(Sensor):
    def read(self):
        return 101.3

temp_sensor = TemperatureSensor("Engine Room")
pressure_sensor = PressureSensor("Boiler")

temp_reading = temp_sensor.read()
pressure_reading = pressure_sensor.read()

📤 Output: 25.5
📤 Output: 101.3


Comparison Table

Concept Parent Class Child Class
Inherits methods Provides base methods Receives all parent methods
Adds new methods No new methods added Can add its own methods
Overrides methods Original method stays Can replace parent method
Calls parent method Not applicable Uses super() to call parent
Practical use General template Specialized version for specific needs

🎯 Context Introduction

In object-oriented programming, inheritance allows us to create new classes (child models) that reuse, extend, and modify the behavior of existing classes (parent templates). Think of it like a blueprint: you have a basic house design (parent class), and you create specialized versions like a house with a garage or a house with a sunroom (child classes). This approach saves time, reduces code duplication, and makes your code more organized and maintainable.


⚙️ What is Inheritance?

  • Parent Class (Base Class): The original template that contains common attributes and methods.
  • Child Class (Derived Class): A new class that inherits everything from the parent and can add or override features.
  • Key Benefit: Write once, reuse many times. Changes to the parent automatically reflect in all children.

🛠️ Creating a Parent Class Template

A parent class serves as the foundation. It defines the shared characteristics that all child classes will inherit.

  • Define a class with common attributes and methods.
  • Use the init method to initialize shared properties.
  • Example structure for a parent class:
  • Class Name: NetworkDevice
  • Attributes: hostname, ip_address, status
  • Methods: connect(), disconnect(), get_info()

📊 Extending with Child Models

Child classes inherit everything from the parent and can:

  • Add new attributes specific to the child.
  • Add new methods unique to the child.
  • Override parent methods to change behavior.
  • Call parent methods using the super() function.

Comparison: Parent vs Child Class

Feature Parent Class Child Class
Attributes Common ones only Inherited + new ones
Methods General purpose Inherited + specialized
Flexibility Fixed template Can be customized
Code Reuse Foundation Extends foundation

🕵️ How Child Models Inherit and Extend

When creating a child model:

  • The child class name is followed by the parent class name in parentheses.
  • The child's init method calls the parent's init using super() to initialize inherited attributes.
  • New attributes are added after the super() call.
  • New methods are defined just like in any class.
  • To override a method, define it with the same name in the child class.

Key points to remember: - A child class can access all non-private parent methods and attributes. - Multiple children can inherit from the same parent. - A child can become a parent for further inheritance (multi-level).


📈 Practical Example Breakdown

Consider a parent class called Server with: - Attributes: name, os, ram_gb - Methods: start(), stop(), restart()

Now create a child class called WebServer that extends Server: - Inherited attributes: name, os, ram_gb - New attributes: port, domain - Inherited methods: start(), stop(), restart() - New method: serve_page() - Overridden method: start() (to also check port availability)

Another child class called DatabaseServer that extends Server: - Inherited attributes: name, os, ram_gb - New attributes: db_type, max_connections - Inherited methods: start(), stop(), restart() - New method: run_query() - Overridden method: stop() (to flush data before shutdown)


🧩 Best Practices for Extending Parent Classes

  • Keep parent classes generic – They should represent common functionality.
  • Keep child classes specific – Add only what is unique to that model.
  • Use super() consistently – Always initialize parent attributes properly.
  • Avoid deep inheritance chains – More than 2-3 levels can become confusing.
  • Override methods with care – Ensure the overridden method still fulfills the parent's contract.
  • Document your extensions – Explain why a child class exists and what it adds.

✅ Summary

Extending parent class templates into child models is a powerful way to build scalable and maintainable code. By starting with a solid parent template and creating specialized children, you can model real-world relationships cleanly. Remember: parent classes provide the foundation, child classes add the details. This approach keeps your code DRY (Don't Repeat Yourself) and makes future changes easier to manage.

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

Extending parent classes templates into child models lets engineers create specialized versions of a base class that inherit its attributes and methods while adding new features.


🔧 Example 1: Basic Parent Class with a Simple Child

This example shows how a child class inherits a method from its parent class without any changes.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class Car(Vehicle):
    pass

my_car = Car()
result = my_car.move()

📤 Output: Vehicle is moving


🔧 Example 2: Child Class Adding Its Own Method

This example demonstrates a child class that inherits a parent method and adds a brand-new method of its own.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class Car(Vehicle):
    def honk(self):
        return "Beep beep!"

my_car = Car()
move_result = my_car.move()
honk_result = my_car.honk()

📤 Output: Vehicle is moving
📤 Output: Beep beep!


🔧 Example 3: Overriding a Parent Method in the Child

This example shows how a child class can replace a parent method with its own version.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class Bicycle(Vehicle):
    def move(self):
        return "Bicycle is pedaling"

my_bike = Bicycle()
result = my_bike.move()

📤 Output: Bicycle is pedaling


🔧 Example 4: Calling the Parent Method from Inside the Child

This example demonstrates how a child class can extend a parent method by calling the parent version first, then adding extra behavior.

class Vehicle:
    def move(self):
        return "Vehicle is moving"

class ElectricCar(Vehicle):
    def move(self):
        parent_move = super().move()
        return parent_move + " silently"

my_ev = ElectricCar()
result = my_ev.move()

📤 Output: Vehicle is moving silently


🔧 Example 5: Practical Engineering Example — Sensor Base Class

This example shows a practical pattern where engineers create a base sensor class and extend it for different sensor types.

class Sensor:
    def __init__(self, location):
        self.location = location

    def read(self):
        return 0

class TemperatureSensor(Sensor):
    def read(self):
        return 25.5

class PressureSensor(Sensor):
    def read(self):
        return 101.3

temp_sensor = TemperatureSensor("Engine Room")
pressure_sensor = PressureSensor("Boiler")

temp_reading = temp_sensor.read()
pressure_reading = pressure_sensor.read()

📤 Output: 25.5
📤 Output: 101.3


Comparison Table

Concept Parent Class Child Class
Inherits methods Provides base methods Receives all parent methods
Adds new methods No new methods added Can add its own methods
Overrides methods Original method stays Can replace parent method
Calls parent method Not applicable Uses super() to call parent
Practical use General template Specialized version for specific needs