Sitemap

Inheritance in Python

3 min readDec 25, 2024

--

Press enter or click to view image in full size

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 attributes name and age.
  • Method: The display_info method 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 Student class inherits from the Person class using the syntax class Student(Person).
  • Initialization: The __init__ method of the Student class calls the __init__ method of the Person class using super(). It also initializes the student_id attribute.
  • Method Overriding: The display_info method in the Student class overrides the display_info method in the Person class. It calls the parent class's method using super() 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: S12345

5. 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 StudentAthlete class inherits from both Student and Athlete.
  • Initialization: The __init__ method calls the __init__ methods of both parent classes.
  • Method Overriding: The display_info method calls the display_info method from the Student class and the display_sport method from the Athlete class.

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: Basketball

7. 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!

--

--