Inheritance is one of the core concepts in object-oriented programming, and it plays a significant role in Python as well. In Python, inheritance allows you to create a new class that is a modified version of an existing class. The new class is known as a derived class, and the existing class is known as a base class. The derived class inherits all the attributes and methods of the base class, and can also add new attributes and methods of its own.
Why use inheritance?
Inheritance provides a way to reuse code and create a hierarchy of classes that model a real-world scenario. Consider a scenario where you have a class named “Person” that contains information about a person such as name, age, and address. Now, you want to create a class named “Student” that represents a student in addition to the information contained in the Person class. You can create a Student class by inheriting from the Person class, so the Student class automatically contains all the attributes and methods of the Person class.
Syntax of inheritance in Python
To inherit from a base class in Python, you simply list the base class in parentheses after the derived class name. For example:
class BaseClass: # Base class definition class DerivedClass(BaseClass): # Derived class definition
In this example, the DerivedClass inherits from the BaseClass. The DerivedClass can access all the attributes and methods of the BaseClass, including any protected and private attributes and methods.
Overriding methods in Python
In Python, it is possible to override the methods of the base class in the derived class. Overriding a method means creating a new method with the same name in the derived class. When a method is called on an object of the derived class, the method of the derived class is executed, not the method of the base class. For example:
class Person: def __init__(self, name): self.name = name def get_name(self): return self.name class Student(Person): def get_name(self): return "Student: " + self.name person = Person("John Doe") student = Student("Jane Doe") print(person.get_name()) # Output: John Doe print(student.get_name()) # Output: Student: Jane Doe
In this example, the Student class inherits from the Person class, but it overrides the get_name method. When the get_name method is called on a Person object, it returns the name of the person. However, when the get_name method is called on a Student object, it returns the string “Student: ” followed by the name of the student.
The super() function in Python
In Python, you can also call the methods of the base class in the derived class using the super() function. The super() function returns a temporary object of the base class, allowing you to call its methods. For example:
class Person: def __init__(self, name): self.name = name def get_name(self): return self.name class Student(Person): def __init__(self, name, student_id): super().__init__(name) self.student_id = student_id def get_student_id(self): return self.student_id student = Student("Jane Doe", 12345) print(student.get_name()) # Output: Jane Doe print(student.get_student_id()) # Output: 12345
In this example, the Student class inherits from the Person class and adds a new attribute “student_id”. The Student class also overrides the init method to include the student_id argument. To access the init method of the Person class, the super().init(name) line is used. The get_student_id method is added to the Student class to return the student_id attribute.
Multiple inheritance in Python
In Python, a derived class can inherit from multiple base classes. This is known as multiple inheritance. For example:
class BaseClass1: # Base class 1 definition class BaseClass2: # Base class 2 definition class DerivedClass(BaseClass1, BaseClass2): # Derived class definition
In this example, the DerivedClass inherits from both BaseClass1 and BaseClass2. The DerivedClass can access the attributes and methods of both base classes.
Conclusion
Inheritance is a powerful feature of object-oriented programming that allows you to create new classes based on existing classes. In Python, inheritance is achieved by listing the base class in parentheses after the derived class name. It is also possible to override methods in Python and access the methods of the base class using the super() function. Multiple inheritance is also supported in Python, allowing a derived class to inherit from multiple base classes. By understanding inheritance in Python, you can write more effective and efficient code, and model real-world scenarios more accurately.