Inheritance in Python
Inheritance is a fundamental concept in object-oriented programming that allows a class (child class) to inherit attributes and methods from another class (parent class). This promotes code reusability and establishes a relationship between classes.
Inheritance syntax
class BaseClass:
# ...
# ...
# ...
class DerivedClass(BaseClass):
# ...
# ...
# ...
The super() function
The super() function is used to call methods from a parent class from within a method of a child class.
Base and derived classes
Parent Class: A class whose properties and methods are inherited by another class is known as a base class, superclass or parent class.
Child Class: A derived class (also known as a child class or subclass) is a class that inherits from a base class. It can extend or modify the functionality of the base class by adding new attributes or methods or by overriding existing ones.
Child class Characteristics:
1. Inherits all methods and properties from the base class.
2. Can have additional properties and methods specific to the derived class.
3. Can override base class methods to provide specialized behaviour.
Types of Inheritance
Types of Inheritance depend upon the number of child and parent classes involved. There are four types of inheritance in Python:
1. Single inheritance
class Person:
def __init__(self, name):
self.name = name
def person_info(self):
print(f"Hello, my name is {self.name}")
class Student(Person):
def study(self):
print(f"{self.name} is studying")
student = Student("Bob")
student.person_info() # Method inherited from Person
student.study() # Method specific to Student
2. Multiple inheritance
class Person:
def __init__(self, name):
self.name = name
def person_info(self):
print(f"Hello, my name is {self.name}")
class Employee:
def __init__(self, job_title):
self.job_title = job_title
def work(self):
print(f"{self.job_title} is working")
class Manager(Person, Employee):
def __init__(self, name, job_title):
Person.__init__(self, name)
Employee.__init__(self, job_title)
manager = Manager("Bob", "Project Manager")
manager.person_info() # Method inherited from Person
manager.work() # Method inherited from Employee
3. Multilevel inheritance
class Person:
def __init__(self, name):
self.name = name
def person_info(self):
print(f"Hello, my name is {self.name}")
class Employee(Person):
def __init__(self, name, job_title):
super().__init__(name)
self.job_title = job_title
def work(self):
print(f"{self.job_title} is working")
class Manager(Employee):
def __init__(self, name, job_title, department):
super().__init__(name, job_title)
self.department = department
def manage(self):
print(f"{self.name} manages the {self.department} department")
manager = Manager("Alice", "Team Lead", "Engineering")
manager.person_info() # Method inherited from Person
manager.work() # Method inherited from Employee
manager.manage() # Method specific to Manager
4. Hierarchical inheritance
class Person:
def __init__(self, name):
self.name = name
def person_info(self):
print(f"Hello, my name is {self.name}")
class Student(Person):
def study(self):
print(f"{self.name} is studying")
class Teacher(Person):
def teach(self):
print(f"{self.name} is teaching")
student = Student("Alice")
teacher = Teacher("Bob")
student.person_info() # Method inherited from Person
student.study() # Method specific to Student
teacher.person_info() # Method inherited from Person
teacher.teach() # Method specific to Teacher
Sources
1. Types of inheritance Python
2. Inheritance