Inheritance in Python
Understanding Inheritance in Python
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit attributes and methods from another class. This promotes code reusability and a hierarchical class structure. In Python, inheritance enables you to create a new class (child class) that inherits properties and behaviors from an existing class (parent class). This article explores the concept of inheritance in Python with examples and explanations.
1. What is Inheritance?
Inheritance is a mechanism where one class (child or derived class) inherits attributes and methods from another class (parent or base class). The child class can access and modify the properties of the parent class, allowing for code reuse and the creation of more complex data models.
2. Defining a Parent Class
Let’s start by defining a simple parent class called Person.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")Explanation:
- Initialization: The
__init__method initializes the attributesnameandage. - Method: The
display_infomethod prints the person's name and age.
3. Creating a Child Class
Now, let’s create a child class called Student that inherits from the Person class.
Example:
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def display_info(self):
super().display_info()
print(f"Student ID: {self.student_id}")Explanation:
- Inheritance: The
Studentclass inherits from thePersonclass using the syntaxclass Student(Person). - Initialization: The
__init__method of theStudentclass calls the__init__method of thePersonclass usingsuper(). It also initializes thestudent_idattribute. - Method Overriding: The
display_infomethod in theStudentclass overrides thedisplay_infomethod in thePersonclass. It calls the parent class's method usingsuper()and adds additional information.
4. Using Inheritance
Let’s create an instance of the Student class and use the inherited and overridden methods.
Get Lavdev Awasthi’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
student = Student("Alice", 20, "S12345")
student.display_info()Output:
Name: Alice, Age: 20
Student ID: S123455. Multiple Inheritance
Python supports multiple inheritance, where a class can inherit from more than one parent class. This can be useful in certain scenarios but should be used with caution to avoid complexity.
Example:
class Athlete:
def __init__(self, sport):
self.sport = sport
def display_sport(self):
print(f"Sport: {self.sport}")
class StudentAthlete(Student, Athlete):
def __init__(self, name, age, student_id, sport):
Student.__init__(self, name, age, student_id)
Athlete.__init__(self, sport)
def display_info(self):
Student.display_info(self)
Athlete.display_sport(self)Explanation:
- Inheritance: The
StudentAthleteclass inherits from bothStudentandAthlete. - Initialization: The
__init__method calls the__init__methods of both parent classes. - Method Overriding: The
display_infomethod calls thedisplay_infomethod from theStudentclass and thedisplay_sportmethod from theAthleteclass.
6. Using Multiple Inheritance
Let’s create an instance of the StudentAthlete class and use the inherited and overridden methods.
Example:
student_athlete = StudentAthlete("Bob", 21, "S54321", "Basketball")
student_athlete.display_info()Output:
Name: Bob, Age: 21
Student ID: S54321
Sport: Basketball7. Benefits of Inheritance
- Code Reusability: Inheritance allows you to reuse code by inheriting attributes and methods from existing classes.
- Hierarchical Structure: It helps in creating a hierarchical structure of classes, making the code more organized and easier to manage.
- Extensibility: You can extend the functionality of existing classes by adding new attributes and methods in the child class.
8. Conclusion
Inheritance is a powerful feature in Python that promotes code reusability, extensibility, and organization. By understanding and leveraging inheritance, you can create more efficient and maintainable programs. Whether you’re creating a simple class hierarchy or dealing with multiple inheritance, mastering this concept is essential for effective object-oriented programming in Python.
Happy coding! 🧑💻
Feel free to ask if you have any other questions or need further assistance!









