Inheritance :
Inheritance
means inheriting the properties of others.
For
example, Son gets some properties from his father, this means that he inherits
the properties of his father. We can say that the father is the base while the
children inherit or derives his properties.
In the
same way, when a class inherits the properties of other class is called as
derived class while the class from which the properties are inherit are called
as base class.
So, Inheritance means a class (derived class) is inheriting properties from another class (the base class). When an object of the derived class is declared, in order to create it, firstly the constructor of the base class is invoked and then, the constructor of the derived class is called.
NEED OF INHERITANCE
1.
To
represent real-world relationships well, which ensure the closeness with real-world
models.
2.
To
provide reusability, which means faster development time, easier maintenance,
and easy to extend.
3.
To
supports transitivity, which means if a class B inherits the property of
another class A, then all the other classes which inherited by class B will
automatically inherits the properties of class A,
DIFFERENT FORMS OF INHERITANCE
The
different forms of inheritance are –
1. 1. Single
Inheritance
2. 2. Multiple
Inheritance
3. 3. Hierarchical
Inheritance
4. 4. Multilevel
Inheritance
5. 5. Hybrid
Inheritance
Single Inheritance
When a subclass or derived class inherits only
from one base class, then it is called Single Inheritance.
Multiple Inheritance
When a subclass or derived class inherits from
multiple base classes, then it is called Multiple Inheritance.
Hierarchical Inheritance
When many subclasses or derived classes
inherit from single base class, then it is called Hierarchical Inheritance.
Multilevel Inheritance
When a subclass or derived class inherit from
single base class and another class is also derived from that derived class,
then it is called Multilevel Inheritance.
Hybrid Inheritance
It is the combination of two or more than two
inheritance forms defined above are called Hybrid Inheritance.
NOTE :
Actually a derived class inherits protected and public members of
base class not the private members. That means, the derived class has access
only to the non-private (i.e. protected and public) members of base class.
i,e, the protected members of base class never gets inherit.
Now,
since we know that we can access the non-private members of base class through
derived class by inheritance, so, sometimes, we want the non-private members of
base class acts as private member of derived class, sometimes we want them to act
as protected members of derived class or sometime as public members.
So, Visibility
Modes are defined, so that we can define whether the base class members
should act as public, protected or private members of derived class.
Visibility Mode
Visibility
Mode in the definition of the derived class specifies whether the features of
the base class are privately derived or publicly derived or protected derived.
0 comments:
Post a Comment
Please give your valuable suggestions