Inheritance is a core concept in Object-Oriented Programming (OOP) that allows a class (child class) to reuse the attributes and methods of another class (parent class). This improves code efficiency and maintains a hierarchical relationship between different classes.
Imagine you are building a website called "Dynamic Duniya", which offers different user roles like Admin, Editor, and Viewer. Instead of defining common features separately for each role, you can create a base User class and allow specific roles to inherit from it.
Python supports several types of inheritance:
In single inheritance, a child class inherits from a single parent class.
class User: # Parent class
def __init__(self, name):
self.name = name
def show_details(self):
print(f"User: {self.name}")
class Admin(User): # Child class inheriting from User
def access_dashboard(self):
print(f"{self.name} has admin access to Dynamic Duniya!")
# Creating an object of the Admin class
admin1 = Admin("Rajesh Kumar")
admin1.show_details() # Inherited from User class
admin1.access_dashboard() # Defined in Admin classUser: Rajesh Kumar
Rajesh Kumar has admin access to Dynamic Duniya!The Admin class inherits the show_details() method from the User class.
In multiple inheritance, a child class inherits from more than one parent class.
class Editor:
def edit_content(self):
print("Editing content on Dynamic Duniya...")
class Moderator:
def moderate_comments(self):
print("Moderating comments on articles...")
class ContentManager(Editor, Moderator): # Inheriting from both Editor and Moderator
def manage_platform(self):
print("Managing all content on Dynamic Duniya.")
# Creating an object of ContentManager
content_manager = ContentManager()
content_manager.edit_content() # Inherited from Editor
content_manager.moderate_comments() # Inherited from Moderator
content_manager.manage_platform() # Defined in ContentManagerEditing content on Dynamic Duniya...
Moderating comments on articles...
Managing all content on Dynamic Duniya.The ContentManager class inherits from both Editor and Moderator, gaining access to their methods.
In multilevel inheritance, a child class inherits from another child class, forming a chain.
class Person:
def __init__(self, name):
self.name = name
def show_name(self):
print(f"Name: {self.name}")
class Customer(Person): # Customer inherits from Person
def place_order(self):
print(f"{self.name} placed an order on Dynamic Duniya.")
class PrimeCustomer(Customer): # PrimeCustomer inherits from Customer
def get_prime_discount(self):
print(f"{self.name} received a special Prime discount!")
# Creating an object of PrimeCustomer
customer1 = PrimeCustomer("Neha Sharma")
customer1.show_name() # Inherited from Person
customer1.place_order() # Inherited from Customer
customer1.get_prime_discount() # Defined in PrimeCustomerName: Neha Sharma
Neha Sharma placed an order on Dynamic Duniya.
Neha Sharma received a special Prime discount!The PrimeCustomer class inherits from Customer, which in turn inherits from Person, forming a chain.
A child class can override a method from the parent class to modify its behavior.
class User:
def greet(self):
print("Welcome to Dynamic Duniya!")
class VIPUser(User):
def greet(self): # Overriding the parent method
print("Welcome, valued VIP user of Dynamic Duniya!")
# Creating an object of VIPUser
vip1 = VIPUser()
vip1.greet() # Output: Welcome, valued VIP user of Dynamic Duniya!Welcome, valued VIP user of Dynamic Duniya!The VIPUser class overrides the greet() method of the User class to provide a personalized greeting.
Single Inheritance: One parent, one child class.
Multiple Inheritance: A child class inherits from multiple parent classes.
Multilevel Inheritance: A child inherits from another child class.
Method Overriding: The child class redefines a method of the parent class.
Sign in to join the discussion and post comments.
Sign inPython Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.
Python for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.