Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its super-class. The method in the subclass overrides the method in the super-class. In Python, for method overriding simply define a method with the same name and signature in the subclass, which will override the method in the parent class.
Method Overriding in Python
In Python, method overriding is a concept where a subclass provides a specific implementation of a method that is already defined in its super-class. The overridden method in the subclass replaces the behavior of the parent class’s method. There are different techniques to implement method overriding in Python, along with variations to customize the behavior.
- Basic Method Overriding – Redefine a method in the subclass.
- Using
super()
– Extend the parent class’s method in the subclass. - Overriding Constructors (
__init__
) – Redefine constructors withsuper()
. - Overriding Class Methods – Override class methods using
@classmethod
. - Overriding Static Methods – Override static methods using
@staticmethod
. - Overriding Properties – Customize or extend properties using
@property
. - Overriding Magic Methods – Override special dunder methods (e.g.,
__str__
,__eq__
). - Multiple Inheritance Overriding – Override methods in classes with multiple inheritance.
- Using
super()
in MRO – Handle method calls based on the Method Resolution Order (MRO) in multiple inheritance. - Overriding Private Methods – Technically possible, but involves name mangling.
Basic Method Overriding
For basic method overriding, we define a method in the subclass with the same name and signature as a method in the parent class. The subclass’s method overrides the parent class’s method when called on an instance of the subclass.
In this example, Child classes PythonCourse
and PowerBICourse
inherits the parent class Course
and override its method course_details()
to provide course specific details.
super()
Method Overriding – Using This is another technique for method overriding calling parent class’s method using super()
. We can call the overridden method from the parent class using the super()
function within the subclass method. This allows us to extend the parent class’s functionality.
In this example, Child classes PythonCourse
and PowerBICourse
inherits the parent class Course
and override its method course_details()
to provide course specific details. course_details()
method in child classes calls the parent class method using super().course_details()
and add the generic details in each course.
__init__
)
Method Overriding – Using Constructors (We can override constructors in subclasses by redefining the __init__
method. It’s common to use super().__init__()
to inherit and extend the parent class constructor.
In this example, Child classe PythonCourse
inherits the parent class Course
. __init__
method in child classes calls the parent class method using super().__init__()
and sets the common attribute values for each course. Using the course_details()
method we got all the attribute values of the course.
@classmethod
Method Overriding – Using In previous sections, we have seen the method overriding for class method which were called using the class objects or class instances. e.g we called python_course.course_details()
using the PythonCourse class object.
Similar to that, class methods can also be overridden. Class methods are defined using the @classmethod
decorator and cls
as the first parameter.
- In this example, Child classes
PythonCourse
andPowerBICourse
inherits the parent classCourse
and override its methodcourse_details()
to provide course specific details. course_details()
method in parent class and child classes is annotated with@classmethod
and first parameter iscls
.- This makes
course_details()
method as class method. It means, to call this method we don’t need to create class object. - We can call
course_details()
method directly using the class likePythonCourse.course_details()
andPowerBICourse.course_details()
.
@staticmethod
Method Overriding – Using Static methods (defined using the @staticmethod
decorator) can also be overridden in sub-classes. These methods don’t rely on instance or class references.
- In this example, Child classes
PythonCourse
andPowerBICourse
inherits the parent classCourse
and override its methodcourse_details()
to provide course specific details. course_details()
method in parent class and child classes is annotated with@staticmethod
@staticmethod
annotation makescourse_details()
method as static method. It means, we can call this method using the class and don’t need class object to call this method.- We can call
course_details()
method directly using the class likePythonCourse.course_details()
andPowerBICourse.course_details()
.
@property
Overriding Properties Using Properties in Python can be overridden using the @property
decorator. Sub-classes can redefine or extend property behavior.
- In this example, Child classes
PythonCourse
andPowerBICourse
inherits the parent classCourse
and override its propertycourse_details
to provide course specific details. course_details()
function in parent class and child classes is annotated with@property
@property
annotation makescourse_details
as property of the class, which is overridden in child classes.- Child class property is calling the parent class property using
super().course_details
- We can call
course_details
property on the class objects likepython_course.course_details
andpowerbi_course.course_details
.
Overriding Magic (Dunder) Methods
Magic (or “dunder”) methods (e.g., __str__
, __repr__
, __eq__
) can be overridden to customize object behavior.
- In this example, Child class
PythonCourse
inherits the parent classCourse
and override method__str__
. __str__
method is directly called on the class object. We don’t need to specify its method call.__str__
method is called on the class object when we printcourse
andpython_course
.
Method Overriding – Multilevel Inheritance
In multilevel inheritance, class inheritance is in multiple levels. The child class inherits from a parent class, which itself is a child of another class. Ex. Child
class inherits from Parent
class and Parent
class itself inherits from GrandParent
class.
- The
Shbytes
class contains a methodcourse_info()
. - The
Courses
class inherits fromShbytes
and override its methodcourse_info()
. - The
PythonCourse
class, which inherits fromCourses
, override methodcourse_info()
. PythonCourse
class has override method fromCourses
class andShbytes
class.
super()
in Multiple Inheritance (MRO)
Method Overriding – Using The Method Resolution Order (MRO) is the order in which Python looks for methods when they are called using super()
. We can check the MRO of any class using the __mro__
attribute or the mro()
method.
ClassName.__mro__
=> Returns tuple of class method resolution orderClassName.mro()
=> Returns list of class method resolution order
- In this example
PythonCourse
class inheritsCourse
class which inheritsShbytes
class. course_info()
method override from parent class. This method calls the parent class method usingsuper().course_info()
super()
ensures that all classes in a hierarchy are properly initialized and called in the correct order, especially in the case of multiple inheritance.- It avoids to hardcode the parent class, leading to more maintainable and extendable code.
Private Methods Not Overriden
Private methods (methods with a name starting with __
) can be overridden in sub-classes, though it’s less common. Since they are name-mangled, they are overridden by matching the mangled name. Private methods can be tricky to override due to name mangling, and it’s not recommended to rely on this approach heavily.
In this example, __course_info
method in PythonCourse
class is trying to override private method from Course
class. But, from the output when we called python_course.get_course_info()
we got the response from the parent method itself. Private method is not overridden.
Summary
In this article, we learned about various techniques for Method Overriding in Python. Following topics were covered:
- Method Overriding in Python
- Basic Method Overriding
- Method Overriding – Using super()
- Method Overriding – Using Constructors (__init__)
- Method Overriding – Using @classmethod
- Method Overriding – Using @staticmethod
- Overriding Properties Using @property
- Overriding Magic (Dunder) Methods
- Method Overriding – Multilevel Inheritance
- Method Overriding – Using super() in Multiple Inheritance (MRO)
- Private Methods Not Overriden
Code – Github Repository
All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.