Inheritance is another important concept in OOP, which allows us to create new classes based on other existing classes.
The new class inherits the attributes and methods of the base class and can add its own attributes and methods. In this way, the subclass can “extend” or modify the behavior of the superclass.
Thus, we call:
- Superclass: The class from which methods and attributes are inherited.
- Subclass: The class that inherits methods and attributes from the superclass.
In Python, to make a class inherit from another, we add the name of the superclass in parentheses right after the class definition. Like this:
class subclass(superclass):
# subclass body
Inheritance Example
Let’s see it with a series of examples. First, suppose we have a Student class that inherits from Person.
# Definition of the Person class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def show_information(self):
print(f"Name: {self.name}, Age: {self.age}")
# Definition of the Student class that inherits from Person
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
# Create an instance of Student
student = Student("Luis", 20, "8.7")
student.show_information() # prints 'Name: Luis, Age: 20'
In this example,
Studentinherits fromPersonusingclass Student(Person)- The
super()function is used to call the constructor of the base classPerson, which allows initializingnameandage.
Method Overriding
In this example, Student overrides the show_information method of Person.
# Definition of the Student class that inherits from Person
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
# Override the show_information method
def show_information(self):
print(f"Name: {self.name}, Age: {self.age}, Grade: {self.grade}")
# Create an instance of Student
student = Student("Luis", 20, "8.7")
student.show_information() # prints 'Name: Luis, Age: 20, Grade: 8.7'
Here, Student overrides the show_information method of Person to include additional information about the grade. This is achieved by defining a new show_information method in the Student class.
Calling Superclass Methods
The derived class can call methods from its base class using the super() method. In fact, it is common to do this with the constructor method __init__ to initialize the attributes of the base class.
But any method of the base class can be invoked. Let’s see it with an example.
# Definition of the Student class that inherits from Person
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age) # Call to the constructor of the base class
self.grade = grade
def show_information(self):
super().show_information(self) # Call to a method of the base class
print(f"Grade: {self.grade}")
# Create an instance of Student
student = Student("Luis", 20, "8.7") # prints 'Name: Luis, Age: 20, Grade: 8.7'
student.show_information()
In this example,
super().__init__(name, age)invokes the constructor ofPerson, to initialize thenameandagefields.super().show_information(self)invokes theshow_informationmethod ofPerson, and then adds more actions (in the example, aprint).
