What is multilevel inheritance in Python?

What is multilevel inheritance in Python?

a) Inheritance involving multiple levels of hierarchy
b) Inheritance with multiple base classes
c) Inheritance with multiple derived classes
d) Inheritance without any base class

Answer:

a) Inheritance involving multiple levels of hierarchy

Explanation:

Multilevel inheritance in Python occurs when a class is derived from another derived class, creating a multi-level hierarchy. Each level of inheritance adds more functionality or specificity.

class Grandparent:
def greet(self):
print("Hello from Grandparent")

class Parent(Grandparent):
def greet(self):
print("Hello from Parent")

class Child(Parent):
def greet(self):
print("Hello from Child")

# Creating an object of Child class
child = Child()
child.greet()  # Output: Hello from Child

In this example, the Child class inherits from Parent, which in turn inherits from Grandparent. The Child class can access the attributes and methods of both Parent and Grandparent classes.

Multilevel inheritance helps in organizing classes in a hierarchical structure, making the code more modular and easier to maintain.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top