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
π‘ 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 π
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! π