1) Single Inheritance
This is the simplest type of inheritance.
An inheritance relationship, with only one super class and one sub class.
2) Hierarchical Inheritance
An inheritance relationship, with only one super class and multiple sub classes.
3) Multi-Level Inheritance
An inheritance relationship, with only one super class and multiple sub classes, and also extended with another sub class from the first sub class.
In other words, a class acts as super class and sub class simultaneously.
4) Multiple Inheritance
An inheritance relationship, with only multiple super classes and only one sub class.
“Multiple Implementation Inheritance” is not supported by C#, but “Multiple Interface Inheritance” is supported by C#.
5) Hybrid Inheritance
An inheritance relationship that contains a combination of any other two types of inheritances.
Ex:
Multiple + Hierarchical Inheritance
Multiple + Multi Level Inheritance etc.
Note: Other OOP languages like C++ support multiple inheritance. But C# and VB.NET doesn’t support multiple inheritance to avoid some practical problems while developing future GUI applications like windows forms applications, web sites etc.
But instead, C# and VB.NET supports “multiple interface inheritance”, because it doesn’t cause any practical problems.
Implementation steps for Inheritance
Define the super class
class superclassname
{
//data members of super class
//methods of super class
}
Define the sub class
class subclassname : superclassname
{
//data members of sub class
//methods of sub class
}
Construct the object for sub class
SubClass obj = new SubClass();
Invoke methods
obj.SuperClassMethod();
obj.SubClassMethod();
Example
We start with an example:
class A
{
void Hello()
{
Console.WriteLine(“Hello, World!”);
}
}
class B:A
{
void Hai()
{
Console.WriteLine(“Hai to all!”);
}
}
In the above example, we have two classes called “A” and “B”. The inheritance relationship has been established between these classes. Now, the “A” class is called as “Super” class and “B” class is called as “Sub” class. All the members of “A” class are exactly copied into “B” class. Finally, whenever you construct an object for the “B” class, you can access both “Hello()” method and “Hai()” method also from that object. This is what we learned up-to-now.
But sometimes, some of the super class members may not be accessible directly from the sub class’s object. This kind of nature depends on the “visibility modifiers”, we use in the super class, while we declare the super class members.