Why Use Inheritance?
1. Code Reusability:
o Avoid rewriting the same logic in multiple places by sharing functionality across classes.
2. Extensibility:
o Build upon existing classes without modifying their code.
3. Hierarchy Representation:
o Model "is-a" relationships in real-world problems (e.g., a Dog is a type of Animal).
4. Overriding and Customization:
o Customize or override behavior for specific cases while retaining shared functionality.
Basic Syntax of Inheritance
class ParentClass:
# Parent class attributes and methods
pass
class ChildClass(ParentClass):
# Inherits ParentClass attributes and methods
pass
Real-Life Analogy
Imagine a Vehicle class:
• All vehicles share common attributes like speed and color, and methods like move() and stop().
• A Car class inherits these common features but may have additional attributes like num_doors or methods like play_music().
• Similarly, a Bike class may inherit the same features but add a type_of_handle() method.
Examples of Inheritance in Python
1. Single Inheritance
A child class inherits from one parent class.
class Animal:
def speak(self):
return "I make a sound"
class Dog(Animal):
def speak(self):
return "Woof!"
# Usage
animal = Animal()
dog = Dog()
print(animal.speak()) # Output: I make a sound
print(dog.speak()) # Output: Woof!
2. Multilevel InheritanceA class inherits from another class, which itself inherits from a third class.
class Vehicle:
def move(self):
return "Vehicle is moving"
class Car(Vehicle):
def move(self):
return "Car is driving on the road"
class SportsCar(Car):
def move(self):
return "SportsCar is zooming on the highway"
# Usage
sports_car = SportsCar()
print(sports_car.move()) # Output: SportsCar is zooming on the highway
3. Hierarchical InheritanceMultiple classes inherit from the same parent class.
class Animal:
def speak(self):
return "I make a sound"
class Cat(Animal):
def speak(self):
return "Meow!"
class Bird(Animal):
def speak(self):
return "Chirp!"
# Usage
cat = Cat()
bird = Bird()
print(cat.speak()) # Output: Meow!
print(bird.speak()) # Output: Chirp!
4. Multiple InheritanceA class inherits from multiple parent classes.
class Engine:
def start(self):
return "Engine started"
class Wheels:
def roll(self):
return "Wheels are rolling"
class Car(Engine, Wheels):
pass
# Usage
car = Car()
print(car.start()) # Output: Engine started
print(car.roll()) # Output: Wheels are rolling
Overriding Methods
Child classes can override methods from the parent class to provide specific behavior.
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
return "Hello from Child"
# Usage
child = Child()
print(child.greet()) # Output: Hello from Child
Using super() to Call Parent Methods
The super() function is used to call a method from the parent class, ensuring the parent’s functionality is still available.
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
parent_message = super().greet()
return f"{parent_message} and Hello from Child"
# Usage
child = Child()
print(child.greet()) # Output: Hello from Parent and Hello from Child
Real-World Example: Employee Hierarchy
Imagine a company's employee structure where all employees share common attributes like name and id. Managers and Developers have additional unique features.
class Employee:
def __init__(self, name, id):
self.name = name
self.id = id
def get_details(self):
return f"Name: {self.name}, ID: {self.id}"
class Manager(Employee):
def __init__(self, name, id, team_size):
super().__init__(name, id)
self.team_size = team_size
def get_details(self):
return f"{super().get_details()}, Team Size: {self.team_size}"
class Developer(Employee):
def __init__(self, name, id, programming_language):
super().__init__(name, id)
self.programming_language = programming_language
def get_details(self):
return f"{super().get_details()}, Programming Language: {self.programming_language}"
# Usage
manager = Manager("Alice", 101, 10)
developer = Developer("Bob", 102, "Python")
print(manager.get_details()) # Output: Name: Alice, ID: 101, Team Size: 10
print(developer.get_details()) # Output: Name: Bob, ID: 102, Programming Language: Python
Key Types of Inheritance in Python
| Type | Description | Example |
|---|---|---|
| Single Inheritance | A child class inherits from one parent class. | Dog from Animal. |
| Multilevel Inheritance | A class inherits from another child class, forming a chain of inheritance. | SportsCar from Car. |
| Hierarchical Inheritance | Multiple classes inherit from the same parent class. | Cat, Bird from Animal. |
| Multiple Inheritance | A class inherits from more than one parent class. | Car from Engine and Wheels. |
Best Practices for Inheritance
- Use Inheritance for "Is-a" Relationships:
o Only use inheritance when the child class truly "is-a" type of the parent class. - Favor Composition Over Inheritance:
o In some cases, use composition (has-a relationships) instead of inheritance to avoid tight coupling. - Avoid Deep Inheritance Hierarchies:
o Keep inheritance levels shallow to reduce complexity. - Use super() for Method Overriding:
o Ensure parent class methods are not lost when overriding in child classes. - Document Class Hierarchies:
o Make the relationships between classes clear in the code documentation.
Conclusion
Inheritance is a powerful feature in Python that allows classes to share attributes and methods, promoting code reuse and extensibility. By understanding and applying inheritance, you can create well-structured, maintainable, and scalable code.
Summary
• What is Inheritance?
o A mechanism for one class (child) to inherit attributes and methods from another class (parent).
• Benefits:
o Code reuse, extensibility, and clear hierarchy.
• Examples:
o Single, multilevel, hierarchical, and multiple inheritance.