What is inheritance? What are different types of
inheritance? Explain advantages of using inheritance
Ans
Inheritance:-Inheritance means using
the Pre-defined Code This is very Main Feature of OOP With the advantage of
Inheritance we can use any code that is previously created. With the help of
inheritance we uses the code that is previously defined but always Remember, We
are only using that code but not changing that code. With
the Advent of inheritance we are able to use pre-defined code and also able to
add new code. All the pre-defined code is reside into the form of classes if we
want to use that code then we have to inherit or extend that class.
The Class that is Pre-defined is called as Base or super
Class and the class which uses the Existing Code is known as derived or sub
class The Various Types of Inheritance those are provided by C++ are as
followings:
1. Single
Inheritance
2. Multilevel
Inheritance
3. Multiple
Inheritance
4. Hierarchical
Inheritance
5. Hybrid
Inheritance
In Inheritance Upper Class whose code we are actually
inheriting is known as the Base or Super Class and Class which uses the Code
are known as Derived or Sub Class.
Single Inheritance
when a single derived class is created from a single base
class then the inheritance is called as single inheritance.
Different types of inheritance
Single Inheritance Example Program
2. Hierarchical Inheritance
when more than one derived class are created from a single
base class, then that inheritance is called as hierarchical inheritance.
Hierarical Inheritance
3. Multi Level Inheritance
when a derived class is created from another derived class,
then that inheritance is called as multi level inheritance.
Multi level Inheritance
4. Hybrid Inheritance
Any combination of single, hierarchical and multi level
inheritances is called as hybrid inheritance.
Hybrid inheritance in C#
5. Multiple Inheritance
when a derived class is created from more than one base class
then that inheritance is called as multiple inheritance. But multiple
inheritance is not supported by .net using classes and can be done using
interfaces.
Multiple inheritance in C#
Multiple Inheritance
Example
Handling the complexity that causes due to multiple
inheritance is very complex. Hence it was not supported in dotnet with class
and it can be done with interfaces.
advantages of using inheritance
- Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked.
- Saves time and effort as the main code need not be written again
- Inheritance allows the use of an already-developed class to write a new class of the same kind.
- In the development of a new project/program, it is possible to define classes and inherit these from the appropriate base class. It helps to reduce source code size and hence improve readability and maintainability of the system.
- It is possible to implement dynamic polymorphism by using inheritance.
Enhancement of programs
becomes easy by using inheritance
No comments:
Post a Comment