In Python, Inheritance is a fundamental pillar of Object-Oriented Programming (OOP) that allows a class (called a Child or Derived class) to acquire the properties and behaviors (attributes and methods) of another class (called a Parent or Base class).
Inheritance represents an "is-a" relationship. For example, a Car is-a Vehicle, and a Manager is-a Employee. This structure allows you to build a hierarchy of classes that move from general to specific.
Code Reusability: You don't have to rewrite the same logic for every new class. You write it once in the Parent class and reuse it in all Child classes.
Extensibility: You can add new features to a Child class without modifying the Parent class.
Maintainability: If a bug exists in the shared logic, you only need to fix it in the Parent class.
The super() function is used to give the child class access to methods and properties of the parent class. It is most commonly used inside the __init__ method to ensure the parent class is initialized properly.
Single Inheritance: A child class inherits from one parent class.
Multiple Inheritance: A child class inherits from more than one parent class.
Multilevel Inheritance: A child class inherits from a parent, which in turn inherits from another class (Grandparent $\rightarrow$ Parent $\rightarrow$ Child).
Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
The simplest form where one class inherits from another.
# Parent Class class Animal: def __init__(self, name): self.name = name def breathe(self): print(f"{self.name} is breathing.") # Child Class class Dog(Animal): def bark(self): print(f"{self.name} says Woof!") # Usage my_dog = Dog("Buddy") my_dog.breathe() # Inherited method my_dog.bark() # Child's own method
Sometimes, a child class needs to perform the parent's initialization plus its own.
class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def work(self): print(f"{self.name} is working.") class Developer(Employee): def __init__(self, name, salary, language): # Use super() to call the Parent constructor super().__init__(name, salary) self.language = language # Method Overriding: Changing parent behavior def work(self): print(f"{self.name} is coding in {self.language}.") dev = Developer("Ankit", 80000, "Python") dev.work()
| Term | Definition |
| Base Class | The class being inherited from (Parent). |
| Derived Class | The class that inherits from the Base class (Child). |
| Method Overriding | Redefining a parent method inside the child class. |
| super() | Built-in function to call parent methods. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION