🌱 Python OOP β€” Single Inheritance

Introduction 🌟

**Single Inheritance** is the simplest form of inheritance in Python. It allows a **child class** to inherit from **one parent class**, gaining its attributes and methods. This promotes **code reuse**, **clean structure**, and **modular design**.

Note

πŸ’‘ Child Class = Derived Class
πŸ’‘ Parent Class = Base Class
πŸ’‘ In Single Inheritance β†’ One Parent β†’ One Child

1. Basic Example of Single Inheritance 🧱

basic_single_inheritance.py

class Parent:
    def display(self):
        print("This is the Parent class")

class Child(Parent):
    pass

c = Child()
c.display()

βœ” Child inherits the display() method
βœ” No need to rewrite parent methods

2. Adding New Methods in Child Class ✨

child_methods.py

class Parent:
    def display(self):
        print("Parent class method")

class Child(Parent):
    def show(self):
        print("Child class method")

c = Child()
c.display()
c.show()

βœ” Child can have its own unique methods too.

3. Overriding Parent Methods (Polymorphism) πŸ”„

override_method.py

class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        print("Hello from Child")

c = Child()
c.greet()

βœ” Same method name, new behavior
βœ” Demonstrates polymorphism in inheritance

4. Using super() to Call Parent Methods 🧠

super_example.py

class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        super().greet()
        print("Hello from Child")

c = Child()
c.greet()

βœ” super() allows you to extend parent functionality instead of replacing it

5. Constructor Inheritance & super() in __init__ πŸš€

constructor_inheritance.py

class Parent:
    def __init__(self, name):
        self.name = name

class Child(Parent):
    def __init__(self, name, age):
        super().__init__(name)
        self.age = age

c = Child("Sathish", 23)
print(c.name, c.age)

βœ” Parent constructor is reused
βœ” Child class extends functionality with new attributes

6. Real-World Example β€” Employee System πŸ§‘β€πŸ’Ό

employee_example.py

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def details(self):
        print(f"Name: {self.name}, Salary: {self.salary}")

class Developer(Employee):
    def __init__(self, name, salary, language):
        super().__init__(name, salary)
        self.language = language

    def show_language(self):
        print("Coding in:", self.language)

d = Developer("Sathish", 50000, "Python")
d.details()
d.show_language()

βœ” Child class inherits common details
βœ” Adds specialized behavior

7. Example: Vehicle β†’ Car πŸš—

vehicle_example.py

class Vehicle:
    def start(self):
        print("Vehicle starting...")

class Car(Vehicle):
    def start(self):
        super().start()
        print("Car engine roaring...")

c = Car()
c.start()

8. Benefits of Single Inheritance 🌟

  • βœ” Reduces code repetition
  • βœ” Improves code structure & readability
  • βœ” Easy to maintain and extend
  • βœ” Encourages modular and logical design
  • βœ” Foundation for advanced OOP concepts

9. When to Use Single Inheritance? 🎯

  • βœ” When you want a simple parent β†’ child relationship
  • βœ” When child needs most features of parent
  • βœ” When extending functionality with minimal changes

10. Common Mistakes ❌

  • ❌ Forgetting to call super().__init__() when overriding constructor
  • ❌ Overriding methods unnecessarily
  • ❌ Adding unrelated behavior inside child class

Conclusion πŸŽ‰

>>β€œSingle Inheritance is the first building block of OOP β€” allowing you to reuse and extend functionality with elegance.” ✨

You now fully understand Single Inheritance in Python! Want the next topic? Try Multilevel Inheritance, Multiple Inheritance, Hierarchical Inheritance, Constructor Overriding, or MRO. Just tell me! 😊