In this Python oops program, we will create a class with single inheritance. The program demonstrates inheritance in Python. The ChildClass
inherits the parent_method
from the ParentClass
and adds its own method child_method
.
Class with single inheritance
Steps to solve the program
- In this program, we demonstrate single inheritance in Python, where a child class inherits from a single parent class.
- The ChildClass inherits the parent_method from the ParentClass, allowing the object obj of the ChildClass to access and invoke both the parent class method (parent_method) and its own class method (child_method).
- This concept of inheritance allows for code reuse and enables the child class to extend or override the functionality of the parent class.
- The program output will be: It first prints “Parent method” because obj.parent_method() invokes the method from the parent class. Then it prints “Child method” because obj.child_method() invokes the method from the child class.
class ParentClass:
def parent_method(self):
print("Parent method")
class ChildClass(ParentClass):
def child_method(self):
print("Child method")
# Create an object of the child class
obj = ChildClass()
obj.parent_method()
obj.child_method()
Output:
Parent method
Child method