Introduction to Inheritance in C#
Inheritance in C# is the process of acquiring all the properties of one class into another class. There are two classes referred to as base class and derived class. The base class is also known as a parent class, and the properties or methods of this class we want to inherit to another class.
The derived class is known as the child class that is used to inherit the properties and methods of the base class or parent class. It helps in reusing the same code again, and no need to define the same properties again and again.
Inheritance is one of the powerful concepts or fundamental attributes of object-oriented programming language. It is widely used in all the OOPs based programming language. Its main purpose is to use the same code again. The example of the Basic Structure of Inheritance is given below:
class BaseClass { }
class ChildClass: BaseClass {}
Types of Inheritance in C#
There are different types of Inheritance in C#:
1. Single Level Inheritance
In Single inheritance, there is only one base class and one derived class. It means the child class will inherit the properties of the parent class and use them.
Example:
class BaseClass
{
public void hello()
{
Console.WriteLine("Parent's Hello Method");
}
}
class ChildClass : BaseClass
{
public void World()
{
Console.WriteLine("Child's World Method");
}
}
2. Multilevel Inheritance
In this type of inheritance, there is only one base class, and multiple derived class are available. If a class is created by using another derived class is known as multilevel inheritance.
Example:
class BaseClass
{
public void hello()
{
Console.WriteLine("Parent's Hello Method");
}
}
class ChildClass : BaseClass
{
public void World()
{
Console.WriteLine("Child's World Method");
}
}
class SecondChildClass : ChildClass
{
public void Hi()
{
}
}
3. Multiple Inheritance
In this type of inheritance, this can be achieved with the help of multiple interfaces, not with a class. In simple words, C# does not support multiple inheritances, but if you want to achieve it, then it can be achieved with the help of interfaces only.
Example:
Interface A {}
Interface B {}
Class C: A, B {}
4. Hierarchical Inheritance
In this type of inheritance, there is one parent class, and the other derived classes inherit the same parent class to achieve this inheritance.
Example:
class BaseClass
{
public void hello()
{
Console.WriteLine("Parent's Hello Method");
}
}
class ChildClass : BaseClass
{
public void World()
{
Console.WriteLine("Child's World Method");
}
}
class SecondChildClass : BaseClass
{
public void Hi()
{
}
}
Advantages of Inheritance in C#
Below are the advantages of Inheritance given.
- It helps in using the same code again means code reusability.
- It reduces code redundancy.
- It helps in reading the code more comfortably.
- It also reduces the size of the source code and file.
- It helps in providing the extensibility to code.
- The code is easy to manage as it divided into classes of the base class and child class.
- Private members are not accessed in derived class when base class members are inherited by the derived class.
- It does not support multiple inheritances but can be achieved through interfaces.
- It helps in achieving the polymorphism that allows an object to represent more than one type.
- It helps in dividing the large code into small pieces.
Features of Inheritance
Following are the features of Inheritance described.
- Inheritance can be used to prevent the direct instantiation of the class, and to achieve this, the abstract keyword has been used.
- The members of the base class can be accessed in derived class except for private key members.
- The members of the base class can be inherited in the derived class except for the constructor and destructor as well.
- In C#, the virtual methods of a base class need to use an override keyword in the derived class.
- In C#, to prevent the inheritance of the class that can be declared with the sealed keyword.
- If the inherited members need to be hidden with the same name and signature in the derived class, then that method needs to be defined with the new keyword.
Why use Inheritance and How it makes it easy to work?
Inheritance is used when the same code needs to be used in another class. So, instead of writing the same code, again and again, there is a concept of object-oriented programming that is Inheritance that helps in using the same functionality like methods or properties of one class to another class. With the help of semicolon (:), we can inherit the members of the base class to child class or derived class.
It makes it easy to work on because it helps in avoiding the confusion of method calling from which class method is getting called. It makes the code reusability and makes the file lighter in weight with less number of lines of source code. This makes the code less redundant and more flexible to use in different classes. It keeps the structure of the program that helps in reading the code easily.
Conclusion
- Inheritance is the most widely used concept of object-oriented programming in all the OOPs based language and so as in C#. It helps the developer to achieve many things and makes the code smoother and readable.
- Polymorphism is the other oops concept that can be achieved with the help of Inheritance only. Most of the problems have been resolved with these two concepts going hand in hand.
- In Inheritance, the base class and derived class are tightly coupled as if there is any change in base class; then automatically, all the child class will get affected. Inheritance needs to be used very carefully as if the data members are not properly used and the memory is allocated to them, then it affects the performance of the application.
- There are different levels of inheritance that is used in programming or developing an application. It can be implemented in any type of application like web-based or desktop-based applications. It depends on the developer how and where to use inheritance as it gives a lot of flexibility, features and helps to achieve the things.
Recommended Articles
This has been a guide to Inheritance in C#. Here we discussed the basic concept, types, advantages, and features of Inheritance in C#. You can also go through our other suggested articles to learn more-