What is Inheritance?
This concept of Inheritance leads to the concept of polymorphism.
Features or Advantages of Inheritance:
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.
Syntax
The above makes sample have access to both public and protected variables of base class example. Reminder about public, private and protected access specifiers:
The output of the above program is
50
200
In the above example, the derived class is sample and the base class is example The derived class defined above has access to all public and private variables. Derived classes cannot have access to base class constructors and destructors. The derived class would be able to add new member functions, or variables, or new constructors or new destructors. In the above example, the derived class sample has new member function f1( ) added in it. The line:
creates a derived class object named as s. When this is created, space is allocated for the data members inherited from the base class exforsys and space is additionally allocated for the data members defined in the derived class sample.
The base class constructor example is used to initialize the base class data members and the derived class constructor sample is used to initialize the data members defined in derived class.
In real life a child inherits or gets different type of properties from his parent. These properties can be either biological (height, skin color, color of eyes etc) or monetory (Land, House, Money etc). Since these properties were not exactly earned by the child ,rather he got them from his parent all such properties will be called inherited properties and process will be known as Inheritance.
Similarly in OOPS, 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 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:
- 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.
- 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.
Increases Program Structure which results in greater reliability.
- Polymorphism
Syntax
class derived_classname: access specifier baseclassnameFor example, if the base class is example and the derived class is sample it is specified as:
|
The above makes sample have access to both public and protected variables of base class example. Reminder about public, private and protected access specifiers:
- 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.
Inheritance Example:
|
The output of the above program is
50
200
In the above example, the derived class is sample and the base class is example The derived class defined above has access to all public and private variables. Derived classes cannot have access to base class constructors and destructors. The derived class would be able to add new member functions, or variables, or new constructors or new destructors. In the above example, the derived class sample has new member function f1( ) added in it. The line:
|
creates a derived class object named as s. When this is created, space is allocated for the data members inherited from the base class exforsys and space is additionally allocated for the data members defined in the derived class sample.
The base class constructor example is used to initialize the base class data members and the derived class constructor sample is used to initialize the data members defined in derived class.
0 comments:
Post a Comment