Example with User, AdminUser, and ManagerUser Classes:
class User:
def __init__(self, user_id, username, email):
self.user_id = user_id
self.username = username
self.email = email
def display_info(self):
print(f"User ID: {self.user_id}, Username: {self.username}, Email: {self.email}")
class AdminUser(User):
def __init__(self, user_id, username, email, role):
super().__init__(user_id, username, email)
self.role = role
# Overriding the display_info method for AdminUser
def display_info(self):
print(f"Admin User - User ID: {self.user_id}, Username: {self.username}, Email: {self.email}, Role: {self.role}")
class ManagerUser(User):
def __init__(self, user_id, username, email, team):
super().__init__(user_id, username, email)
self.team = team
# Overriding the display_info method for ManagerUser
def display_info(self):
print(f"Manager User - User ID: {self.user_id}, Username: {self.username}, Email: {self.email}, Team: {self.team}")
Explanation:
AdminUser
and ManagerUser
inherit from User
, gaining its attributes and methods.display_info()
method to provide their own versions, tailoring the output to their specific information.display_info()
on an object of either subclass, the appropriate overridden version is executed, demonstrating polymorphism.Benefits of Method Overriding:
Relation to SOLID
principles
The SOLID
principle related to method overriding is the "Open/Closed Principle" (O in SOLID
).
The Open/Closed Principle states that a class should be open for extension but closed for modification.
In the context of method overriding, this principle encourages designing classes and methods in a way that allows for extension (new functionality) without modifying the existing code.
When you override a method in a subclass, you are extending the behavior without modifying the original method in the superclass. This aligns with the Open/Closed Principle, as it enables you to introduce new functionalities by creating new subclasses or overriding methods without altering the existing codebase. This promotes code stability, scalability, and maintainability in the face of changing requirements.