Monday, September 9, 2013

What is inheritance? What are the types of Inheritance ?

Inheritance is the concept in which one type (class/interface) derives data member and member functions from another type is called as inheritance. The type which derives the members from another type is called as derived type and the type from which derives is called as base type.

C# supports two types of inheritance:
  • Implementation inheritance
  • Interface inheritance

Implementation inheritance involves code from one class that is reused in another, while interface inheritance implies the inheritance of only the signatures of functions from one type to another.

Main advantage of inheritance is reusability of the code.

Example: We have created two classes one is base class called ClsBase and other is Child class called ClsChild, this ClsChild class is derived from ClsBase Class. Now lets see the following code,

                          Fig 1
Fig 2

In Fig 1 we are creating object of base class so it is showing properties of base class only. But in Fig 2 we are creating object of child class, it exposes data member and member function of child class as well as base class i.e means child class inherits the features of base class called Inheritance.

No comments:

Post a Comment