10. Problem to create a class with multiple inheritance in Python

In this Python oops program, we will create a class with multiple inheritance. The program demonstrates multiple inheritance in Python. The ChildClass inherits methods from both ParentClass1 and ParentClass2, and an object created from the child class can access and invoke methods from all the parent classes as well as its own methods.

Class with multiple inheritance

Steps to solve the program
  1. In this program, we demonstrate multiple inheritance in Python, where a child class inherits from multiple parent classes.
  2. The ChildClass inherits both the parent_method1 from ParentClass1 and the parent_method2 from ParentClass2. This allows the object obj of the ChildClass to access and invoke methods from both parent classes, as well as its own class method.
  3. Multiple inheritance provides flexibility in reusing code from multiple sources and allows for complex class hierarchies.
  4. The program output will be:It first prints “Parent method 1” because obj.parent_method1() invokes the method from ParentClass1. Then it prints “Parent method 2” because obj.parent_method2() invokes the method from ParentClass2. Finally, it prints “Child method” because obj.child_method() invokes the method from the child class.

				
					class ParentClass1:
    def parent_method1(self):
        print("Parent method 1")

class ParentClass2:
    def parent_method2(self):
        print("Parent method 2")

class ChildClass(ParentClass1, ParentClass2):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.parent_method1()
obj.parent_method2()
obj.child_method()
				
			

Output:

				
					Parent method 1
Parent method 2
Child method
				
			

Related Articles

Python class with Single Inheritance.

Python Class with Multilevel Inheritance.

Leave a Comment