Inheritance in Python

Welcome to Inheritance

Welcome back! Now that you have a solid understanding of classes and objects in Python, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).

Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.

Introduction to Inheritance

Inheritance is a way to establish a relationship between a new class (derived class) and an existing class (base class). The derived class inherits properties and behaviors (methods) from the base class.

Here’s a simple example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display(self):
        print(f"Name: {self.name}, Age: {self.age}")

class Student(Person):
    def __init__(self, name, age, major):
        super().__init__(name, age)
        self.major = major

    def display(self):
        super().display()
        print(f"Major: {self.major}")

if __name__ == "__main__":
    student = Student("Bob", 25, "Computer Science")
    student.display()
    # Output:
    # Name: Bob, Age: 25
    # Major: Computer Science

In this snippet, the Student class inherits from the Person class. It reuses the name and age attributes and the display method from the Person class, and it adds a new attribute, major.

You can think of inheritance like a university system. Imagine a university where all students are automatically considered persons, because they have fundamental attributes such as a name and age just like any other person. However, not every person is a student; for someone to be a student, they need to have additional attributes, such as a major. In the same way, a derived class (like Student) inherits basic attributes and methods from the base class (like Person) but can also have additional attributes and functionality of its own.

How Inheritance Works

When you declare a derived class, you specify the base class it inherits from using the class DerivedClass(BaseClass) syntax. The derived class extends or overrides the functionality of the base class.

In our example, the Person class serves as the base class, while the Student class is the derived class, inheriting name and age attributes from the Person class. The Student class also introduces an additional attribute, major, and overrides the display method to provide information about the major.

Notice how Student.display() calls Person.display() using super() to reuse the base class functionality before adding its own details. By overriding, you have the ability to completely redefine the method's function in the derived class, which means the base class method will not automatically be executed. If you still want the base class's method to be part of the derived class's method execution, you have to explicitly call it using super(). This is crucial for maintaining the fundamental behavior of the base class while introducing new or extended features in the derived class.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal