Inheritance in OOP C#






4.11/5 (6 votes)
Inheritance in OOP C#
Introduction
Previously, we have discussed about the Abstraction and Encapsulation and their difference in this blog. Today, we will go for the other two properties of OOP in C#, these are Inheritance and Polymorphism.
Description
As we all know, OOPs has 4 properties like:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Already we have discussed about the first two, abstraction & encapsulation. Let's discuss about the other two, Inheritance and Polymorphism.
Inheritance
Inheritance means getting some thing (properties) as heredity. To get that, we need a hierarchical structure. And that is provided by OOP using Inheritance. Here in inheritance, we have a concept of base class and sub class. The base class is also known as parent class, super class, etc. and the sub class is also known as child class. The properties (variables) and methods are accessible to the sub class, but keep in mind that only the public ones. The private methods or variables are not accessible from the child class. And the methods are accessible can be called without creating the object of that super class.
To inherit a class from another class, you have to extend the sub class to super class. Follow the example given below:
class ClassSuper
{
public void MethdoTestPublic()
{
Console.WriteLine("ClassTest.MethdoTestPublic");
}
private void MethdoTestPrivate()
{
Console.WriteLine("ClassTest.MethdoTestPublic");
}
}
class ClassSub : ClassSuper // Inheriting from ClassSuper
{
public void MethodTest()
{
MethdoTestPublic(); // calling the super class method
MethdoTestPrivate(); // calling the super class private method [Error]
Console.WriteLine("ClassTest2.MethodTest");
}
}
Here, ClassSub
is extending the ClassSuper
to get access to the methods and properties of the super class.
Type of Inheritance:
- Single Inheritance
- Multiple Inheritance
- Multi Level Inheritance
- Hierarchical Inheritance
Single Inheritance
The above example is one of the examples of Single Inheritance.
class ClassSuper
{
// Do your code
}
class ClassSub : ClassSuper // Inheriting from ClassSuper
{
// Do your code
}
Multi Level Inheritance
Multi level inheritance is the joining of two or more single inheritance. Like Sub Class 2 is inheriting Sub Class 1 and Sub Class 1 is inheriting Super Class. If you watch closely, there are two single inheritances present. One is Sub Class 1 -> Sub Class 2 and second one is Sub Class 1 -> Super Class.
class ClassSuper
{
// Do your code
}
class ClassSub1 : ClassSuper // Inheriting from ClassSuper
{
// Do your code
}
class ClassSub2 : ClassSub1 // Inheriting from ClassSub2
{
// Do your code
}
Hierarchical Inheritance
In case of Hierarchical interface, we have multiple sub classes which are inheriting one super class. Like here Sub Class 1 and Sub Class 2, both are inheriting Super Class. This is Hierarchical Inheritance.
class ClassSuper
{
// Do your code
}
class ClassSub1 : ClassSuper // Inheriting from ClassSuper
{
// Do your code
}
class ClassSub2 : ClassSuper // Inheriting from ClassSuper
{
// Do your code
}
Multiple Inheritance
Multiple Inheritance is just the opposite of Hierarchical Inheritance. Here one sub class is inheriting multiple Super Classes. Like any other Object Oriented Programming languages, C# also doesn't support Multiple Inheritance. To achieve that, we have to go for Interface, which we will discuss later.
Like here, the only Sub Class is inheriting the two Super Classes (Super Class 1 and Super Class 2). This architecture is not possible with C# classes. So we are taking help of Interface to do so.
interface ISuper1
{
// Do your code
}
interface ISuper2
{
// Do your code
}
class ClassSub : ISuper1, ISuper2 // Inheriting from ISuper1 and ISuper2
{
// Do your code
}
Advantages of Inheritance
- Reusability: Use the base class
public
methods and properties from the base class, without defining or creating any instance. - Extensibility: Extend the base class methods logic in the sub class without rewriting the same.
- Data hiding: Base class can keep some data
private
so that it cannot be altered by the derived class. - Override: Base class methods can be overridden by sub class methods to reuse in a meaningful way.
Disadvantages of Inheritance
- Tightly Coupled: In inheritance, both the classes (sub and super class) are tightly coupled. So if any change occurs in super class, it will affect in the base class.
- Sometimes, a lot of data remains unused in the hierarchy. So memory wastage can happen in case of inheritance.