First time user?    |

#### Select chapter

```
Inheritance:
The feature enables us to include the features of a class to another class.
Purpose of Inheritance:
-> To improve ease of maintenance is the main purpose of inheritance by improving effectiveness in re-usability of code.
Example 1:
Let us consider 3 types of calculators for example and try to understand what the purpose of inheritance is.

Sample Code:
public class TypicalCalc
{
// method 2 - Subtraction
// method 3 - Division
// method 4 - Multiplication
}
public class  ScientificClac
{
// variable 2 - degree
// method 2 - Subtraction
// method 3 - Division
// method 4 - Multiplication
// method 5 - Logarithm
// method 6 - Exponential
// method 7 - Trignometric
}
public class ProgrammerCalc
{
// variable - hexa
// variable - decimal
// variable - octa
// variable - binary
// method 2 - Subtraction
// method 3 - Division
// method 4 - Multiplication
// method 5 - And
// method 6 - Or
// method 7 - Not
// method 8 - Nor
// method 9 - Xor
}
public class StatisticCalc
{
// variable 1 - exp
// method 2 - Subtraction
// method 3 - Division
// method 4 - Multiplication
// method 5 - Standard Deviation
// method 6 - Sum of n numbers
// method 7 - Factorial
}

Observation:
-> We can observe TypicalCalc, ScientificCalc, ProgrammerCalc and StatisticCalc are having various methods and variables to accomplish certain tasks in arithmetic and trigonometry etc.

-> How to improve re-usability of same Code? (i.e. codes for basic arithmetic operations - Add,Sub,Mul,Div)
- To avoid the repetition of codes let us group all the common methods and variables from all classes in a class names "BasicCalc" and inherit it to all the classes.

-> How it helps in Maintenance?
- If we want to modify any of the common codes in future, then it enough to change the data present in the BasicCalc class instead of changing in all the classes, thus leading to lower effort for maintenance.
In general, it is better to have the common codes in a class with suitable name so that it is easy to inherit to any class wherever we need the common features.
Sophisticated sample code:

public class BasicCalc
{
// method 2 - Subtraction
// method 3 - Division
// method 4 - Multiplication
}
public class TypicalCalc : BasicCalc
{

}
public class ScientificCalc : BasicCalc
{
// variable 2 - degree
// method 5 - Logarithm
// method 6 - Exponential
// method 7 - Trignometric
}
public class ProgrammerCalc : BasicCalc
{
// variable - hex
// variable - decimal
// variable - octa
// variable - binary
// method 5 - And
// method 6 - Or
// method 7 - Not
// method 8 - Nor
// method 9 - Xor
}
public class StatisticCalc : BasicCalc
{
// variable 1 - exp
// method 5 - Standard Deviation
// method 6 - Sum of n numbers
// method 7 - Factorial
}

Hence in C# language terminology, the BasicCalc is class as Base / Parent / Super class and whereas Doctor class is called as Derived / Child / Sub class.
Example 2:

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
new obj = new New();
}
}
public class Common
{
public int a = 0;
public string b = "Hello";
}
public class New : Common
{
public int x = 30;
public bool y = true;
}

The memory allocation for variables present in the above program is given in figure above. In C# strings are always stored in string buffer as a reference type and not in Heap memory like how it is shown here. But strings are handled in a unique way unlike other reference types. Hence to avoid confusion about strings while learning the basics of c# language, here it is shown in Heap memory.
Observation:
1. 'Common' class acts as base class for 'New' class.
2. 'Object' class is inherited by 'Common' class during compilation by C# compiler as Common class is not inheriting from any class.
(It is recommended to refer Object Class topic now and then continue with this topic)

Types of Inheritance in C#:

Single-level Inheritance:

Inheriting one class from another class is known as single-level inheritance. See example 2 for a sample code of single-level inheritance where "Common" class acts as parent class and "New" class acts as child class.

One example for combination of single-level inheritance:

Multi-level Inheritance:

* There are several ways of achieving single level inheritance, inheriting a class which is acting as child to some other class already is known as multi - level inheritance.
* As every child / derived class inherits some of the features / properties / codes from parent / base class, they are represented in growing sequence in the diagram.
* The child / derived class contains more codes when compared to parent / base class (just as real world - child inherits all the properties of parents and grandparents).
* Reminder: Object class acts as the Ultimate parent for any class hierarchy.

Note: Declaring a class will never allocate any memory in RAM. When they are executed (in case of static) or during object creation only memory is calculated and allotted according to the members (variables/methods) coded under the class.
* It is very important to know the sequence of memory allocation of members of class during object creation of child class using 'new' keyword, members of parent class comes into existence first and then the next level child class member (assuming both members and classes are Instance members and Instance Class) just like real world scenario where parents come into existence first. Refer to "Types of classes" in order to understand Instance classes and Static classes for proper understanding.

Multiple inheritance and Hybrid inheritance

* If a class inherits many classes by itself, it is known as multiple inheritance.
* Combination of multiple inheritance is nothing but hybrid inheritance. (Both multiple and hybrid inheritance are not supported by C# language in order to escape ambiguity, confusion and duplication).
* The reason behind the ban of multiple and hybrid inheritance in C# is the diamond problem.

# Let us assume that multiple inheritance is allowed in C# for understanding the problem occurring in the following program:
public class SuperParent : Object
{ public int x = 1;
}
public class SubParent1 : SuperParent
{ public int x = 10;
}
public class SubParent2 : SuperParent
{ public int x = 100;
}
public class Child:SubParent1,Subparent2
{ public int x = 1000;
}

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// new object using parameter-less constructor
Child c = new Child();
Response.Write(c.x); // 1000
// Down-casting for ref of SubParent1 object using c
SubParent1 sub1 = c;
Response.Write(sub1.x); // 10
// Down-casting for ref of SuperParent object using sub1
SuperParent super1 = sub1;
Response.Write(super1.x); // 1
// Down-casting for ref of SubParent2 object using c
SubParent2 sub2 = c;
Response.Write(sub2.x); // 100
// Down-casting for ref of SuperParent object using sub2
SuperParent super2 = sub2;
// Down-casting for ref of SuperParent object using c
SuperParent super3 = c;
}
}

Remember : c, sub1, sub2, super1, super2, super3 are reference variables.(Refer Upcasting and Downcasting topic for complete understanding of casting operations/conversions)

Observation:
From the last line of the program it is very clear that, because of multiple inheritance the reference variable super3 will point to two locations which is absurd and hence creates ambiguity and confusion. This is the reason behind most programming languages don't support multiple inheritance.

```