What is Single Inheritance in Python?

What is Single Inheritance in Python?

Are you confused about inheritance and single inheritance in Python? Stick to this article to understand what it is in Python.

Inheritance is a core and helpful concept of object-oriented programming. Python allows inheritance in multiple ways depending on the defined use case. We have different types in Python, including single, multiple, multilevel, hierarchical, and hybrid inheritance. To learn about the different types of inheritance that can be implemented with Python, you can refer to our more comprehensive guide here.

This article will discuss what single inheritance is in Python, how it works, and a practical example or implementation. So let’s dive deep into it!

 

What is Single Inheritance in Python?

It is one of the most common types of inheritance in Python, where the child or derived class is a single parent or base class. It allows you to inherit the functions and properties from the base class to the derived class, which is a good practice and improves code useability and readability.

Single inheritance in Python

The syntax of inheritance is pretty simple:

class BaseClass():

# Body of base class

class DerivedClass(BaseClass):

# Body of derived class

 

You can access the properties and functionalities of the base class using the object of the derived class. But what differentiates this from the other types in Python is that you can only inherit a class once. Which means there will be a base class and a derived class only. 

To understand it better, let’s see a practical example:

 

Code

# base class

class BaseClass():

    def baseClass(self):

        print("This is a base class")

    

# child class 1

class DerivedClass(BaseClass):

    def derived class(self):

        print("This is a derived class")

        

# creating object of derived class

dc = DerivedClass()




dc.baseClass()

dc.derived class()

 

Output

This is a base class

This is a derived class

 

See, we have instantiated the object of the derived class, which allows us to access the members of the base class as well as itself.

 

 

Conclusion

To summarize this article on single inheritance in Python, we have discussed what it is in Python and how we use it. Inheritance improves code reusability, which is a preferred coding methodology.

Name a scenario that is best suitable for single inheritance in Python.

Total
0
Shares
Leave a Reply

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

Related Posts
Total
0
Share