Type of Inheritance in c sharp

Type of Inheritance

This talk about the number of parent classes a class can have and according to the standards of object oriented programming we have two types of inheritance.

  1. Single Inheritance
  2. Multiple Inheritance
If a class has one immediate parent class to it we call it as single inheritance. When if a class have more than one immediate parent classes to it we call it as multiple inheritance.

In Java and .Net language there is no support for multiple inheritance through class and what they support only single inheritance because multiple suffers from ambiguity problem.

Note: Traditional c++ language supports both single and multiple inheritances also because it is 1st OOP language that came into existence and at that time this problem was anticipated.

we have learn that wherever the child class instance is created child class constructor will implicitly called its parent class constructor for execution where this implicit calling will possible only if the parent classes constructor is parameter less. But at all the parent classes contractor is parameterized then implicit calling can’t be performed because parent classes constructor requires value to the parameters, in such cases it is the responsibility of programmer to explicitly call parent classes constructor from child class using the base ()  keyword and pass the required value for execution.

To test the above add class class1 and write the following code


Now add the class2 and write the following code


In the above case when we are creating the instance of class2 we are passing a value which will first reach the constructor of class2 and these it is transported to class1.
 

No comments:

Post a Comment