One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also 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, the 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.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Base and derived classes
Base class -
A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes
that can reuse the code implicitly inherited from the base class (except constructors and destructors). A programmer can extend base class functionality by
adding or overriding members relevant to the derived class.
A base class may also be called parent class or superclass.
Derived class -
A derived class is a class created or derived from another existing class. The existing class from which the derived class is created through the process
of inheritance is known as a base class or superclass.
Derived classes are used for augmenting the functionality of base class by adding or modifying the properties and methods to suit the requirements of the
specialization necessary for derived class. This allows for defining virtual methods that form the means to implement polymorphism, which allows a group of
objects to work in uniform manner. Thus, the inherent advantages of inheritance and polymorphism like code reuse, faster development, easy maintenance,
etc., are realized.
A derived class is also known as subclass or child class.
Syntax of inherit the class
Class derived-class: access-specifier base class
Access control
Access |
Public |
protected |
private |
Same class |
Yes |
yes |
yes |
Derived classes |
Yes |
yes |
no |
Outside classes |
Yes |
no |
no |
Types of inheritance:
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hybrid inheritance
5. Hierarchical inheritance
Single inheritance : In Single Inheritance there is only one Super Class and Only one Sub Class Means they have one to one Communication between them.
Multilevel inheritance : In Multilevel Inheritance a Derived class can also inherited by another class Means in this When a Derived Class again will be inherited by another Class then it creates a Multiple Levels.
Multiple inheritance : Multiple Inheritances is that in which a Class inherits the features from two Base Classes When a Derived Class takes Features from two Base Classes.
Hierarchical inheritance : Hierarchical Inheritance is that in which a Base Class has Many Sub Classes or When a Base Class is used or inherited by many Sub Classes.
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.
No comments:
Post a Comment