Inheritance is another major aspect in object-oriented programming. By the inheritance we can pass on the functionalities of derive class to base class. A class that inherits from the super class is called base class or often child class. By the inheritance method we can share the information to other class without making it purely public. Here it is to be noted that inherit class can only access the protected data members. So, we don’t have to make all thing public and can hide the information following is the example of inheritance in python by which the concept of private protected and public data members can also be cleared.
We don’t have to initialize the base class we can access only by derived class. The output of the following code is as follows:
Here we can see that private members cannot be accessed.
Now we will see a detailed example of inheritance. In following class there are three classes employee and programmer. The programmer is inherited by the employee and use the functionality of employee class:
We defined a class Employee in which it takes the first name of the employee, last name of the employee and the pay. There is another parameter of appraisal. The employee class have two methods one is getting the full name and other is increase salary. Than we defined another class called programmer. The programmer class inherits from the Employee and take extra parameter of language. We initialize the super constructor.
Than we create the instance pass the parameter and by the instance of programmer we can access the functionalities of Employee to manipulate the values.
The output of the above given code is following;
Similarly, we can do multiple inheritance and execute more than on derived class at a time. Following is the example of multiple inheritance: