Inheritance
The word inheritance means to pass on the features of one generation to the next.
Its like a child inherits some or all characteristics of his parents and develops
new characteristics of his own.
Is case of OOPs, inheritance means to derive a new class from the existing class
without modifying the existing one. The new class is called derived class (or subclass
or child class or descendent class) and the existing or old class is called base
class (or super class or parent class or ancestor class). The derived class can
inherit some or all features (data and functions) of the base class and can add
its own features to extend the functionality of the base class. But in this whole
procedure the base class does not get affected. The process of inheritance is also
known as ‘derivation’.
The concept of inheritance is used at the time when programmer has to use such objects
in his program that have certain features in common like shown below. Consider the
following example-
Suppose we have three objects of an organization- worker, manager and clerk. We
will have to make three classes one for each object. These are shown below-
Click on image for large image
Here these three classes have certain data functions in common. The common data
is - emp_id, ename and salary. The common functions are - get_data ( ) and put_data
( ). If we make these three classes as the way they are, then we will have to write,
debug and compile common data and functions again and again and it will result in
wastage of resources and time. Moreover if we have to add a new feature address
that is common for all classes then we will have to add it to each of these classes.
It means if we have to do some common change that is required for all classes, then
we will have to make that change to all classes even though it is common. It is
very time consuming and tedious job.
The solution for this is to make a class and put all the common data and functions
in that class. So we will make class employee and will put the common features like
emp_id, ename, salary, get_data ( ) and put_data ( ) in the class. This class will
act as base class. And now we will derive other classes worker, manager and clerk
from it and will add uncommon features to these classes i.e. the features that are
not common to all these three classes like hr_wages, units_assigned, units_made,
calc_salary ( ) belong only to worker class; reports, report_details ( ) to manager
class and sales, purchase, audit ( ) to clerk class. It is shown below-
Now if we have to add any new feature, like address, that is common to all three
classes, that will be put into employee class instead of adding separately to worker,
manager and clerk classes. Only distinct features are required to be added separately
to these classes.
Inheritance works only in one direction. The derived class inherits the features
of the base class but base class does not know anything about the derived class.
The derived class can inherit some or all the properties of the base class and can
also pass on the inherited properties to the class which will be inherited from
it. It depends on the accessibility level of the data and functions of the base
class and the way the base class is inherited. The accessibility levels and the
way of derivation can be- Private, Protected and Public.
From the above figure it is clear that only the Protected and Public members are
inherited and not the Private members. And inherited members will be available for
further inheritance only if they are inherited in public way or protected way otherwise
not.
The advantages of inheritance are-
- With it the already written code can be reused. Thus the programmer does not need
to do the same thing again and again and he can concentrate on new things.
- The quality and reliability of the program is increased because the thing, which
we reuse, has been used and tested many times.
- We can easily modify the program.
http://
http://
Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com
Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=27
Click here to go on website
|