Python Tutorial
About Lesson

In this Python lesson we want to learn about Python Class Inheritance, also we are going to create a practical example in Python Class Inheritance.

 

 

What is Class Inheritance in Python ?

Class inheritance is one of the most important concepts in object oriented programming, basically a class inheritance allows us to create a relationship between two or more classes. now in object oriented programming one class can inherit attributes and methods from another class.

 

In this image you can see that we have two classes, the first one is our Class A and we have some variables with one method in this class, in term of class inheritance we can call this class a Base Class, Parent Class or Super class. we have another Class B, now this class extends or inherit from Class A, and for this class we can call Derived Class, Child Class or Sub Class. as we have already said when a class extends from the the base class, the derived class can access to all attributes and methods of the base class. for example in here our Class B child class can access to the fname and lname attributes of the Class A super class.

Python Class Inheritance
Python Class Inheritance

 

 

 

Now let’s create a practical example on class inheritance in python.

So in the above code we have a base class and we have a derived class, in the base class we have some instance attributes and a method for printing the variables. now our derived class can access to all attributes and method of the base class, for example Student class can access to the name, email variables and also full_info() method.

 

 

If you run the code this will be the result.

Python super Class
Python super Class

 

 

What are the Benefits of Python Class Inheritance

Python class inheritance allows you to create new classes based on existing ones, and it provides different benefits for software development. these are some main benefits of using class inheritance in Python:

  1. Code reuse: by inheriting or extending from an existing class you can reuse the code that has already been written, it reduces the amount of code that you need to write and making your code more efficient and easier to maintain.
  2. Extensibility: Inheritance allows you to create new classes that extend the functionality of existing classes, adding new features or modifying existing ones. this makes it easy to adapt your code to new requirements or to build on top of existing functionality.
  3. Polymorphism: Inheritance is key feature of polymorphism, which allows you to write code that works with different types of objects in the same way. by defining new classes that inherit from existing ones, you can create objects that are polymorphic and it makes it easy to write code that works with different types of objects in a generic way.
  4. Modular design: Inheritance allows you to break down your code into smaller and more manageable pieces, each of these pieces can be developed and tested independently. this makes it easier to maintain and modify your code over time and it makes it easier to work with large and complex systems.
  5. Easier maintenance: Inheritance makes it easier to maintain your code over time because it allows you to make changes to the base class that will be reflected in all derived classes. this makes it easy to fix bugs or add new features without having to modify each individual class separately.

 

In result we can say that Python class inheritance provides number of benefits for software development, including code reuse, extensibility, polymorphism, modular design and easier maintenance. by using inheritance effectively, you can create code that is more efficient, easier to maintain and more adaptable to changing requirements over time.