Tutorial Name: Codes With Pankaj
Website: www.codeswithpankaj.com
- Introduction to Python Inheritance
- Understanding the Basics of Inheritance
- Creating a Simple Inheritance Example
- Types of Inheritance in Python
- Method Overriding
- Using super() Function
- Inheritance and Encapsulation
- Practical Examples of Inheritance
- Best Practices for Using Inheritance
- Conclusion
Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) in Python. It allows you to create a new class (child class) that inherits attributes and methods from an existing class (parent class). This promotes code reusability and helps to maintain a clean and organized codebase.
In this tutorial, we'll explore the concept of inheritance in detail, understand different types of inheritance, learn about method overriding, and see how the super() function is used in inheritance. We'll also look at practical examples and best practices for implementing inheritance in Python.
Inheritance in Python is a mechanism that allows one class to inherit attributes and methods from another class. The class that inherits is called the child class or subclass, and the class from which it inherits is called the parent class or superclass.
Example:
class Parent:
def parent_method(self):
print("This is a parent class method.")
class Child(Parent):
pass
# Creating an object of the Child class
child_obj = Child()
child_obj.parent_method()Explanation:
- In this example, the
Childclass inherits theparent_method()from theParentclass.
- Code Reusability: You can reuse the code from the parent class in the child class.
- Simplifies Code Maintenance: Changes made in the parent class are automatically reflected in the child class.
- Extensibility: You can add new features to a child class without modifying the parent class.
- Parent Class (Superclass): The class whose properties and methods are inherited by another class.
- Child Class (Subclass): The class that inherits properties and methods from the parent class.
Let's create a simple example to demonstrate inheritance.
Example:
class Animal:
def sound(self):
print("This is an animal sound.")
class Dog(Animal):
def bark(self):
print("Woof! Woof!")
# Creating an object of the Dog class
dog = Dog()
dog.sound() # Inherited from Animal class
dog.bark() # Defined in Dog classExplanation:
- The
Dogclass inherits thesound()method from theAnimalclass. - Additionally, the
Dogclass defines its own method,bark().
Python supports different types of inheritance, allowing you to design complex class hierarchies. Let's explore the main types of inheritance.
In single inheritance, a child class inherits from one parent class.
Example:
class Parent:
def method(self):
print("Parent class method.")
class Child(Parent):
def method(self):
print("Child class method.")
child = Child()
child.method() # Output: Child class method.In multiple inheritance, a child class inherits from more than one parent class.
Example:
class Parent1:
def method1(self):
print("Parent1 class method.")
class Parent2:
def method2(self):
print("Parent2 class method.")
class Child(Parent1, Parent2):
pass
child = Child()
child.method1() # Output: Parent1 class method.
child.method2() # Output: Parent2 class method.Explanation:
- The
Childclass inherits methods from bothParent1andParent2.
In multilevel inheritance, a child class inherits from a parent class, and another child class inherits from that child class.
Example:
class Grandparent:
def grandparent_method(self):
print("Grandparent class method.")
class Parent(Grandparent):
def parent_method(self):
print("Parent class method.")
class Child(Parent):
pass
child = Child()
child.grandparent_method() # Inherited from Grandparent class
child.parent_method() # Inherited from Parent classExplanation:
- The
Childclass inherits methods from bothParentandGrandparent.
In hierarchical inheritance, multiple child classes inherit from a single parent class.
Example:
class Parent:
def parent_method(self):
print("Parent class method.")
class Child1(Parent):
pass
class Child2(Parent):
pass
child1 = Child1()
child2 = Child2()
child1.parent_method() # Inherited from Parent class
child2.parent_method() # Inherited from Parent classHybrid inheritance is a combination of two or more types of inheritance.
Example:
class Base:
def base_method(self):
print("Base class method.")
class Parent1(Base):
pass
class Parent2(Base):
pass
class Child(Parent1, Parent2):
pass
child = Child()
child.base_method() # Inherited from Base classExplanation:
- The
Childclass inherits fromParent1andParent2, both of which inherit from theBaseclass.
Method overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class. This allows the child class to modify or enhance the behavior of the inherited method.
Example:
class Parent:
def method(self):
print("Parent class method.")
class Child(Parent):
def method(self):
print("Child class method (overridden).")
child = Child()
child.method() # Output: Child class method (overridden).Explanation:
- The
Childclass overrides themethod()from theParentclass, providing its own implementation.
The super() function in Python is used to call a method from the parent class in the child class. This is useful when you want to extend the functionality of a parent class method in the child class.
Example:
class Parent:
def method(self):
print("Parent class method.")
class Child(Parent):
def method(self):
super().method() # Call the parent class method
print("Child class method (extended).")
child = Child()
child.method()Output:
Parent class method.
Child class method (extended).
Explanation:
- The
super().method()calls the method from the parent class, and the child class adds additional functionality.
Inheritance works seamlessly with encapsulation in Python. Enc
apsulation is the practice of hiding internal details of a class and restricting access to them.
Example:
class Parent:
def __init__(self):
self._protected_var = "Protected Variable"
def method(self):
print("Parent class method.")
class Child(Parent):
def display(self):
print("Accessing:", self._protected_var)
child = Child()
child.display()Explanation:
- The
_protected_varis a protected variable, but it can still be accessed in the child class.
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposited {amount}, New Balance: {self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrawn {amount}, New Balance: {self.balance}")
else:
print("Insufficient funds")
class SavingsAccount(BankAccount):
def add_interest(self, rate):
interest = self.balance * rate
self.deposit(interest)
print(f"Interest Added: {interest}")
# Example usage
account = SavingsAccount("John Doe", 1000)
account.deposit(500)
account.add_interest(0.05)Explanation:
- The
SavingsAccountclass inherits fromBankAccountand adds a new methodadd_interest().
class Employee:
def __init__(self, name, position):
self.name = name
self.position = position
def show_details(self):
print(f"Employee: {self.name}, Position: {self.position}")
class Manager(Employee):
def __init__(self, name, position, team_size):
super().__init__(name, position)
self.team_size = team_size
def show_details(self):
super().show_details()
print(f"Team Size: {self.team_size}")
# Example usage
manager = Manager("Alice", "Project Manager", 10)
manager.show_details()Explanation:
- The
Managerclass inherits fromEmployeeand extends theshow_details()method to include team size.
- Use Inheritance When It Makes Sense: Inheritance should represent an "is-a" relationship. For example, a
Dogis a type ofAnimal, so inheritance is appropriate. - Avoid Deep Inheritance Hierarchies: Deep hierarchies can make code difficult to understand and maintain. Favor composition over inheritance when appropriate.
- Override Methods Carefully: When overriding methods, ensure that the new implementation is consistent with the original method's purpose.
- Use
super()to Extend Parent Methods: When you need to extend the behavior of a parent method, usesuper()to avoid code duplication.
Inheritance is a powerful feature in Python that allows you to reuse code, extend functionality, and create organized class hierarchies. By understanding different types of inheritance, method overriding, and the use of super(), you can effectively implement inheritance in your Python projects.
For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.
End of Tutorial
This comprehensive tutorial on Python inheritance covers everything from basic concepts to advanced topics like method overriding and the super() function. With practical examples and best practices, you'll be able to implement inheritance in your Python projects efficiently.