What is single inheritance in Python?

What is single inheritance in Python?

a) A class inheriting from multiple classes
b) A class inheriting from only one base class
c) A class with a single method
d) A function returning a single value

Answer:

b) A class inheriting from only one base class

Explanation:

Single inheritance in Python occurs when a class inherits from only one base class. This means that the derived class can access the attributes and methods of the single base class.

class Parent:
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 the Parent class. The Child class can access the greet() method from the Parent class, but it also has the option to override it with its own implementation.

Single inheritance is straightforward and useful when there is a clear hierarchical relationship between classes.

Leave a Comment

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

Scroll to Top