In this python oops program, we will create a class with method overriding in python. The program showcases method overriding in Python
Method overriding in Python
Steps to solve the program
- The ParentClass is a base class or parent class that defines a method called parent_method. This method prints “Parent method”.
- The ChildClass is a derived class or child class that inherits from ParentClass. It defines its own method called parent_method, which overrides the method in the parent class. This method prints “Child method (overrides parent)”.
- An object obj is created using the ChildClass.
- The obj.parent_method() call invokes the parent_method defined in the child class. Since the child class overrides the method from the parent class, the overridden method in the child class is executed. It prints “Child method (overrides parent)”.
class ParentClass:
def parent_method(self):
print("Parent method")
class ChildClass(ParentClass):
def parent_method(self):
print("Child method (overrides parent)")
# Create an object of the child class
obj = ChildClass()
obj.parent_method()
Output:
Child method (overrides parent)
Related Articles
Write a Python Class Program with an Abstract method.
Write a Python Class program to create a class with data hiding.
Python Class Structure for School Management System.
Write a Python Class Structure for Employee Management Application.
Write a Python Class with @property decorator.
Write a Python Class structure with module-level Import.