Inheritance in Python
Inheritance is a key concept in object-oriented programming (OOP) that allows one class (called
the child or derived class) to acquire attributes and methods from another class (called the
parent or base class). This promotes code reusability and establishes a clear relationship
between classes.
A child class can also extend or override the behavior of its parent class.
Python supports several forms of inheritance, each suited for different use cases:
Types of Inheritance in Python
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
1. Single Inheritance
A child class inherits from a single parent class.
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal): # Child class
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Dog barks
2. Multiple Inheritance
A child class inherits from more than one parent class.
class Animal:
def speak(self):
print("Animal makes a sound")
class Mammal:
def walk(self):
print("Mammal walks on land")
class Dog(Animal, Mammal):
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Dog barks
dog.walk() # Output: Mammal walks on land
3. Multilevel Inheritance
Inheritance occurs in a chain where a class is derived from another derived class.
class Animal:
def speak(self):
print("Animal makes a sound")
class Mammal(Animal):
def walk(self):
print("Mammal walks on land")
class Dog(Mammal):
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Dog barks
dog.walk() # Output: Mammal walks on land
4. Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
def speak(self):
print("Dog barks")
class Cat(Animal):
def speak(self):
print("Cat meows")
dog = Dog()
dog.speak() # Output: Dog barks
cat = Cat()
cat.speak() # Output: Cat meows
5. Hybrid Inheritance
A combination of multiple types of inheritance (e.g., multiple + hierarchical).
class Animal:
def speak(self):
print("Animal makes a sound")
class Mammal:
def walk(self):
print("Mammal walks on land")
class Dog(Animal):
def speak(self):
print("Dog barks")
class Bat(Mammal):
def fly(self):
print("Bat flies in the air")
class FlyingDog(Dog, Bat):
def speak(self):
print("Flying Dog barks and flies")
flying_dog = FlyingDog()
flying_dog.speak() # Output: Flying Dog barks and flies
flying_dog.walk() # Output: Mammal walks on land
flying_dog.fly() # Output: Bat flies in the air
Mini Project: Employee Management System
Implementation
class Employee:
def __init__(self, name, emp_id, salary):
self.name = name
self.emp_id = emp_id
self.salary = salary
def display_info(self):
print(f"Employee ID: {self.emp_id}")
print(f"Employee Name: {self.name}")
print(f"Salary: ${self.salary}")
class FullTimeEmployee(Employee):
def __init__(self, name, emp_id, salary, benefits, work_hours):
super().__init__(name, emp_id, salary)
self.benefits = benefits
self.work_hours = work_hours
def display_info(self):
super().display_info()
print(f"Benefits: {self.benefits}")
print(f"Work Hours: {self.work_hours}")
def yearly_salary(self):
return self.salary * 12
class PartTimeEmployee(Employee):
def __init__(self, name, emp_id, hourly_rate, work_hours):
super().__init__(name, emp_id, 0) # No fixed monthly salary
self.hourly_rate = hourly_rate
self.work_hours = work_hours
def display_info(self):
super().display_info()
print(f"Hourly Rate: ${self.hourly_rate}")
print(f"Work Hours: {self.work_hours}")
def calculate_salary(self):
return self.hourly_rate * self.work_hours
def main():
ft_employee = FullTimeEmployee("Alice Johnson", 101, 5000, "Health Insurance, Paid
Leave", 40)
print("Full-Time Employee Info:")
ft_employee.display_info()
print(f"Yearly Salary: ${ft_employee.yearly_salary()}")
print("\n" + "-"*40 + "\n")
pt_employee = PartTimeEmployee("Bob Lee", 102, 20, 25)
print("Part-Time Employee Info:")
pt_employee.display_info()
print(f"Salary for this month: ${pt_employee.calculate_salary()}")
if __name__ == "__main__":
main()
Explanation of the Code
1. Employee Class (Base Class)
○ Defines attributes like name, emp_id, and salary.
○ Provides a method display_info() to show employee details.
2. FullTimeEmployee (Derived Class)
○ Inherits from Employee.
○ Adds benefits and work_hours.
○ Overrides display_info() to show extra details.
○ Includes yearly_salary() to calculate annual pay.
3. PartTimeEmployee (Derived Class)
○ Inherits from Employee.
○ Uses hourly_rate and work_hours to calculate pay.
○ Overrides display_info().
○ Provides calculate_salary() method for monthly pay.
4. Main Function
○ Creates both full-time and part-time employee objects.
○ Displays their information and salary calculations.