One of the most important concepts in object-oriented
programming is that of inheritance. Inheritance is a mechanism of
reusing and extending existing classes without modifying them, thus
producing hierarchical relationships between them and provides an
opportunity to reuse the code functionality and fast implementation
time.
When creating a class, instead of writing completely new
data members and member functions, programmer can designate that the new
class should inherit the members of an existing class. This existing
class is called the base class, and the new class is referred to as the
derived class.Forms of Inheritance
- Single Inheritance – If a class is derived from a single base class, it is called as single inheritance.
- Multiple Inheritance – If a class is derived from more than one base class, it is known as multiple inheritance.
- Multilevel Inheritance – The classes can also be derived from the classes that are already derived. This type of inheritance is called multilevel inheritance.
- Hierarchical Inheritance – If a number of classes are derived from a single base class, it is called as hierarchical inheritance.
- Hybrid Inheritance – This is a Mixture of two or More Inheritance and in this Inheritance a Code May Contains two or Three types of inheritance in Single Code.
Visibility Mode
It is the keyword that controls the visibility and availability of inherited base class members in the derived class. It can be either private or protected or public.- Private Inheritance – When deriving from a private base class, public and protected members of the base class become private members of the derived class.
- Public Inheritance – When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
- Protected Inheritance – When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
Base class visibility | Derived class visibility | ||
Public derivation | Private derivation | Protected derivation | |
Private | Not inherited | Not inherited | Not inherited |
Protected | Protected | Private | Protected |
Public | Public | Private | Protected |
No comments:
Post a Comment