9. Problem to create a class with single inheritance in Python

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
  1. In this program, we demonstrate single inheritance in Python, where a child class inherits from a single parent class.
  2. 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).
  3. This concept of inheritance allows for code reuse and enables the child class to extend or override the functionality of the parent class.
  4. 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
				
			

Related Articles

Python Class object under syntax if __name__ == ‘__main__’.

Python Class with Multiple Inheritance.

Leave a Comment