Inheritance in Python:
Inheritance is a powerful feature in Python that allows one class to inherit properties and behavior from another class. This helps to reduce code duplication, makes it easier to maintain and extend the behavior of a class, and creates a clear hierarchy of classes.
Benefits of Inheritance
- Code Reusability: Inheritance allows you to reuse code from the superclass in the subclass.
- Easier Maintenance: Inheritance makes it easier to modify or extend the behavior of a class without affecting other classes that inherit from it.
- Improved Readability: Inheritance helps to create a clear hierarchy of classes, making it easier to understand the relationships between classes.
Types of Inheritance:
Single Inheritance*: A subclass inherits from a single superclass.
Example:
class Animal
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
```
- *Multiple Inheritance*: A subclass inherits from multiple superclasses.
Example:
class Animal:
def __init__(self, name):
self.name = name
class Mammal:
def __init__(self, hair_color):
self.hair_color = hair_color
class Dog(Animal, Mammal):
def __init__(self, name, breed, hair_color):
Animal.__init__(self, name)
Mammal.__init__(self, hair_color)
self.breed = breed
```
- *Multilevel Inheritance*: A subclass inherits from a superclass that itself inherits from another superclass.
Example:
class Animal:
def __init__(self, name):
self.name = name
class Mammal(Animal):
def __init__(self, name, hair_color):
super().__init__(name)
self.hair_color = hair_color
class Dog(Mammal):
def __init__(self, name, breed, hair_color):
super().__init__(name, hair_color)
self.breed = breed
- *Hierarchical Inheritance*: A superclass has multiple subclasses that inherit from it.
Example:
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name)
self.color = color
*Hybrid Inheritance:*
A combination of multiple and multilevel inheritance.
*Example:*
```
# Base class 1
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
# Base class 2
class Employee:
def __init__(self, emp_id, salary):
self.emp_id = emp_id
self.salary = salary
def display_emp(self):
print(f"Employee ID: {self.emp_id}, Salary: {self.salary}")
# Base class 3 (inherits from Person)
class Manager(Person):
def __init__(self, name, age, department):
super().__init__(name, age)
self.department = department
def display_manager(self):
print(f"Department: {self.department}")
# Hybrid inheritance class (inherits from Manager and Employee)
class SeniorManager(Manager, Employee):
def __init__(self, name, age, department, emp_id, salary):
Manager.__init__(self, name, age, department)
Employee.__init__(self, emp_id, salary)
def display_senior_manager(self):
self.display()
self.display_manager()
self.display_emp()
# Create an object of SeniorManager
sm = SeniorManager("John Doe", 35, "Sales", "EMP123", 50000)
# Call methods from all base classes
sm.display_senior_manager()
```
Output:
```
Name: John Doe, Age: 35
Department: Sales
Employee ID: EMP123, Salary: 50000
```
Method Overloading and Method Overriding
Method Overloading
Method overloading is a technique that allows you to define multiple methods with the same name but different parameters.
Example:
class Calculator:
def calculate(self, a, b=None):
if b is None:
return a * a
else:
return a + b
calc = Calculator()
print(calc.calculate(5)) # Output: 25
print(calc.calculate(5, 10)) # Output: 15
```
Method Overriding
Method overriding is a feature of inheritance that allows a subclass to provide a different implementation of a method that is already defined in its superclass.
Example:
```
class Animal:
def sound(self):
print("The animal makes a sound")
class Dog(Animal):
def sound(self):
print("The dog barks")
animal = Animal()
animal.sound() # Output: The animal makes a sound
dog = Dog()
dog.sound() # Output: The dog barks