Pages

Wednesday, October 13, 2010

INHERITANCE

Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features or some of the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.


This concept of Inheritance leads to the concept of polymorphism.

Features or Advantages of Inheritance:

1)Reusability:
Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed.
2)Saves Time and Effort:

The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code written can be reused in various situations as needed.

3)Increases Program Structure which results in greater reliability.

General Format for implementing the concept of Inheritance:
class derived_classname: access specifier baseclassname
Properties of access specifier:

  • If a member or variables defined in a class is private, then they are accessible by members of the same class only and cannot be accessed from outside the class. .


  • Public members and variables are accessible from outside the class.


  • Protected access specifier is a stage between private and public. If a member functions or variables defined in a class are protected, then they cannot be accessed from outside the class but can be accessed from the derived class.
  • 0 comments:

     
    Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews