Today we are going to look at the last type of inheritance which is
Multiple inheritance.
Multiple Inheritance
When a derived class inherits more
than one base class, then it is said to be multiple inheritance. This child
class has access to the elements of both the base class. Let us look at an
example.
Consider two base classes as given below.
The class
personal_details
provides provisions for adding personal information like name,age and email
address. The class
educational_details
gives the provisions to add the qualification details of an employee. These
two information must exist for every employee, thus they are common
requirements.
The class
employee
inherits both the classes
personal_details
and
educational_details. It can access all the attributes and functions of the base classes. In
addition, it has a
calculate()
function which calculates the salary of the employees based on their
designation and year of joining (yoj). The function for printing the details
of an employee is given below.
Now we can create the instance of the
employee class
and access all the attributes and functions as follows.
This is called as multiple inheritance. However in any of the inheritance
note that you cannot access the
private variable of the base/parent class. A private variable/function can be accessed
only by the class that owns it. If you try to access it, you will get
object has no attribute attribute_name error.
Tomorrow let us look into the concepts of
overriding.
Next Topic👉
Comments
Post a Comment