What is Python Class Inheritance

In this article we are going to talk about What is Python Class Inheritance,

Class inheritance is one of the most important concepts in Object Oriented Programming,

if you are familiar with other OOP programming languages like Java or C++, so it has the

same concepts, 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.

 

 

So now let’s take a look at this image example.

Python Class Inheritance
Python Class Inheritance

 

If you take a look at the above image, we have created two classes, the first class is

Class A, in this class we have created two variables and also we have a method. in the

term of Object Oriented Programming, we can call this class as base class, parent class

or super class. we have another class that is Class B, and this class inherited from the

Class A, for this class we can call derived class, child class or sub class. now in class

inheritance when a class extends from another class, the derived class can access to all

attributes and methods of the base class. for example in here our Class B can access to

the fname, lname and full_name() method of our Class A or base class.

 

 

 

 

Let’s create a practical example in Python Class Inheritance.

 

 

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 methods of the base class, for example Student class

can access to the nameemail variables and also full_info() method.

 

 

You can see that in here we have created the instance for our Student class not

Person class, and in the Student class we don’t have any attributes like name and

email or a method for printing the information, these are added in the Person class.

 

 

 

If you run the code this will be the result.

What is Python Class Inheritance
What is Python Class Inheritance

 

 

 

 

Leave a Comment