Introduction
Inheritance is one of the fundamental concepts of object-oriented programming (OOP). It allows a class (called the child or subclass) to inherit attributes and methods from another class (called the parent or superclass). Inheritance promotes code reusability and establishes a natural hierarchy between classes.
Key Concepts
Parent Class (Superclass)
The class whose attributes and methods are inherited by another class.
Child Class (Subclass)
The class that inherits attributes and methods from another class.
Syntax
Defining a Parent Class
class ParentClass:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
def method1(self):
return "This is method1 from ParentClass"
Defining a Child Class
class ChildClass(ParentClass):
def __init__(self, attribute1, attribute2, attribute3):
super().__init__(attribute1, attribute2) # Inheriting attributes from ParentClass
self.attribute3 = attribute3
def method2(self):
return "This is method2 from ChildClass"
Example
Let’s consider a real-world example of a vehicle system where we have a generic Vehicle
class and more specific classes like Car
and Bike
that inherit from Vehicle
.
Parent Class: Vehicle
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
return f"Vehicle: {self.brand} {self.model}"
Child Class: Car
class Car(Vehicle):
def __init__(self, brand, model, seating_capacity):
super().__init__(brand, model) # Inheriting attributes from Vehicle
self.seating_capacity = seating_capacity
def display_info(self):
return f"Car: {self.brand} {self.model}, Seating Capacity: {self.seating_capacity}"
Child Class: Bike
class Bike(Vehicle):
def __init__(self, brand, model, bike_type):
super().__init__(brand, model) # Inheriting attributes from Vehicle
self.bike_type = bike_type
def display_info(self):
return f"Bike: {self.brand} {self.model}, Type: {self.bike_type}"
Creating and Using Objects
# Creating objects of Car and Bike classes
my_car = Car("Toyota", "Corolla", 5)
my_bike = Bike("Yamaha", "MT-15", "Sport")
# Displaying information
print(my_car.display_info()) # Output: Car: Toyota Corolla, Seating Capacity: 5
print(my_bike.display_info()) # Output: Bike: Yamaha MT-15, Type: Sport
Types of Inheritance
Single Inheritance
A child class inherits from a single parent class.
class Parent:
pass
class Child(Parent):
pass
Multiple Inheritance
A child class inherits from more than one parent class.
class Parent1:
pass
class Parent2:
pass
class Child(Parent1, Parent2):
pass
Multilevel Inheritance
A child class inherits from a parent class, which in turn inherits from another parent class.
class Grandparent:
pass
class Parent(Grandparent):
pass
class Child(Parent):
pass
Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
pass
Hybrid Inheritance
A combination of two or more types of inheritance.
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
pass
class GrandChild(Child1, Child2):
pass
Real-World Example: Employee Management System
Let’s consider a real-world example of an employee management system where we have a generic Employee
class and more specific classes like FullTimeEmployee
and PartTimeEmployee
that inherit from Employee
.
Parent Class: Employee
class Employee:
def __init__(self, name, employee_id):
self.name = name
self.employee_id = employee_id
def display_info(self):
return f"Employee: {self.name}, ID: {self.employee_id}"
Child Class: FullTimeEmployee
class FullTimeEmployee(Employee):
def __init__(self, name, employee_id, salary):
super().__init__(name, employee_id) # Inheriting attributes from Employee
self.salary = salary
def display_info(self):
return f"Full-Time Employee: {self.name}, ID: {self.employee_id}, Salary: {self.salary}"
Child Class: PartTimeEmployee
class PartTimeEmployee(Employee):
def __init__(self, name, employee_id, hourly_rate):
super().__init__(name, employee_id) # Inheriting attributes from Employee
self.hourly_rate = hourly_rate
def display_info(self):
return f"Part-Time Employee: {self.name}, ID: {self.employee_id}, Hourly Rate: {self.hourly_rate}"
Creating and Using Objects
# Creating objects of FullTimeEmployee and PartTimeEmployee classes
full_time_employee = FullTimeEmployee("Ramesh", "FT123", 60000)
part_time_employee = PartTimeEmployee("Sita", "PT456", 20)
# Displaying information
print(full_time_employee.display_info()) # Output: Full-Time Employee: Ramesh, ID: FT123, Salary: 60000
print(part_time_employee.display_info()) # Output: Part-Time Employee: Sita, ID: PT456, Hourly Rate: 20
Conclusion
Inheritance in Python allows you to create a new class that inherits attributes and methods from an existing class. This promotes code reusability and establishes a natural hierarchy between classes. By understanding and utilizing different types of inheritance (single, multiple, multilevel, hierarchical, and hybrid), you can design robust and scalable object-oriented systems. The real-world examples of a vehicle system and an employee management system demonstrate how inheritance can be applied to model complex systems effectively.