Hello folks....!!!!
Today let us continue with the concept of inheritance's other types.
Multilevel inheritance
- A new class that inherits a derived class exhibits multilevel inheritance.
- For example class B inherits class A, class C inherits B. Now class C has access to the elements of class A and B as its parent B has been derived from class A.
- Let us consider the same example we considered yesterday. The record creation for an organization. For reference look at this post.
The base class is as given below :
Then the child class
employee
derives the base class
person.
Next a class named
manager
inherits the class
employee.
Now the class
manager has
access to the attributes and functions of the classes
person and
employee.
Now the relationship between this classes can be thought of as a manager is
an employee and an employee is a person. So by transitivity a manager is a
person. Thus one object created for the manager class can be used to call
the functions in other classes also.
To avoid too many function call as given above one can follow
class composition
by calling the constructors and functions of another class inside their
functions. Thus the class
employee can
be modified as given below.
The
manager class
also requires slight modifications which is given below.
Now the number of function calls can be reduced. But the output remains the
same.
Next Topic👉
Comments
Post a Comment