Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). In this article, we'll explore inheritance in Python.
Syntax:
class ParentClass:
# Parent class code here
pass
class ChildClass(ParentClass):
# Child class code here
pass
Explanation:
- ParentClass: base class that defines attributes and methods.
- ChildClass(ParentClass): derived class that inherits everything from the parent.
- The child can reuse, extend or override parent methods and attributes.
Why do we need Inheritance
- Promotes code reusability by sharing attributes and methods across classes.
- Models real-world hierarchies like Animal -> Dog or Person -> Employee.
- Simplifies maintenance through centralized updates in parent classes.
- Enables method overriding for customized subclass behavior.
- Supports scalable, extensible design using polymorphism.
Creating a Parent Class
In object-oriented programming, a parent class (also known as a base class) defines common attributes and methods that can be inherited by other classes. These attributes and methods serve as the foundation for the child classes. By using inheritance, child classes can access and extend the functionality provided by the parent class.
Example: This program creates a simple parent class with attributes name and id.
Python
class Person(object):
def __init__(self, name, id):
self.name = name
self.id = id
def Display(self):
print(self.name, self.id)
# Driver code
emp = Person("Elon", 102)
emp.Display()
Explanation:
- The Person class has two attributes: name and id. These are set when an object of the class is created.
- The display method prints the name and id of the person.
Creating a Child Class
A child class (also known as a subclass) is a class that inherits properties and methods from its parent class. The child class can also introduce additional attributes and methods, or even override the ones inherited from the parent.
Example: This program creates a child class Emp that extends functionality of Person.
Python
class Emp(Person):
def Print(self):
print("Emp class called")
Emp_details = Emp("Mayank", 103)
Emp_details.Display() # calling parent class function
Emp_details.Print() # Calling child class function
Explanation: Emp does not define its own __init__. It inherits Person’s constructor directly, so objects of Emp are initialized using the parent’s constructor.
super() Function
super() function is used to call the parent class’s methods. In particular, it is commonly used in the child class’s __init__() method to initialize inherited attributes. This way, the child class can leverage the functionality of the parent class.
Example: Here, Employee uses super() to call the constructor of Person.
Python
# Parent Class: Person
class Person:
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)
# Child Class: Employee
class Employee(Person):
def __init__(self, name, idnumber):
super().__init__(name, idnumber)
Explanation:
- The super() function is used inside the __init__() method of Employee to call the constructor of Person and initialize the inherited attributes (name and idnumber).
- This ensures that the parent class functionality is reused without needing to rewrite the code in the child class.
Add Properties
Once inheritance is established, both the parent and child classes can have their own properties. Properties are attributes that belong to a class and are used to store data.
Example: Here, parent class has name and idnumber and the child class adds salary and post.
Python
# Parent Class: Person
class Person:
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)
# Child Class: Employee
class Employee(Person):
def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber)
self.salary = salary
self.post = post
Explanation:
- Person class has properties name and idnumber.
- Employee class adds properties salary and post.
- The properties are initialized when an object is created and they represent the specific data related to the Person and Employee.
Types of Python Inheritance
Python supports several types of inheritance:
- 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 class is derived from a class which is also derived from another class.
- Hierarchical Inheritance: Multiple classes inherit from a single parent class.
- Hybrid Inheritance: A combination of more than one type of inheritance.
Example: Demonstrating different inheritance types
Python
# 1. Single Inheritance
class Person:
def __init__(self, name):
self.name = name
class Employee(Person):
def __init__(self, name, salary):
super().__init__(name)
self.salary = salary
# 2. Multiple Inheritance
class Job:
def __init__(self, salary):
self.salary = salary
class EmployeePersonJob(Employee, Job):
def __init__(self, name, salary):
Employee.__init__(self, name, salary) # Initialize Employee
Job.__init__(self, salary) # Initialize Job
# 3. Multilevel Inheritance
class Manager(EmployeePersonJob):
def __init__(self, name, salary, department):
EmployeePersonJob.__init__(self, name, salary) # Explicitly initialize EmployeePersonJob
self.department = department
# 4. Hierarchical Inheritance
class AssistantManager(EmployeePersonJob):
def __init__(self, name, salary, team_size):
EmployeePersonJob.__init__(self, name, salary) # Explicitly initialize EmployeePersonJob
self.team_size = team_size
# 5. Hybrid Inheritance (Multiple + Multilevel)
class SeniorManager(Manager, AssistantManager):
def __init__(self, name, salary, department, team_size):
Manager.__init__(self, name, salary, department) # Initialize Manager
AssistantManager.__init__(self, name, salary, team_size) # Initialize AssistantManager
# Creating objects
# Single Inheritance
emp = Employee("John", 40000)
print(emp.name, emp.salary)
# Multiple Inheritance
emp2 = EmployeePersonJob("Alice", 50000)
print(emp2.name, emp2.salary)
# Multilevel Inheritance
mgr = Manager("Bob", 60000, "HR")
print(mgr.name, mgr.salary, mgr.department)
# Hierarchical Inheritance
asst_mgr = AssistantManager("Charlie", 45000, 10)
print(asst_mgr.name, asst_mgr.salary, asst_mgr.team_size)
# Hybrid Inheritance
sen_mgr = SeniorManager("David", 70000, "Finance", 20)
print(sen_mgr.name, sen_mgr.salary, sen_mgr.department, sen_mgr.team_size)
OutputJohn 40000
Alice 50000
Bob 60000 HR
Charlie 45000 10
David 70000 Finance 20
Explanation:
- Single Inheritance: Employee inherits from Person, adding a salary attribute.
- Multiple Inheritance: EmployeePersonJob inherits from both Employee (which already inherits Person) and Job. This allows it to access both name (from Person) and salary (from Job/Employee).
- Multilevel Inheritance: Manager inherits from EmployeePersonJob, which already includes Employee and Job.
- Hierarchical Inheritance: AssistantManager also inherits from EmployeePersonJob, demonstrating multiple child classes inheriting from the same parent.
- Hybrid Inheritance: SeniorManager inherits from both Manager (multilevel) and AssistantManager (hierarchical), combining two inheritance types.
For more details, read this article: Types of inheritance in Python
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice