What is Inheritance in C#?
Inheritance in C# is one of the most important topics when discussing about object-oriented programming. It allows us to define a base class with specific data and functionality and define derived classes that can inherit the properties of the base class.
It is mainly used
- To inherit properties and behavior of another class.
- Reuse and modify the existing class members.
- We can use a sealed keyword to restrict inheritance.
Types Of Inheritance in C#
- Single Level inheritance
- Multi-level inheritances
C# does not support multiple inheritances, it can be achieved through interfaces in c#. There are certain members which are not inherited in the derived classes. Those members are
- Static constructors – To initialize static data of the class
- Instance constructors – These constructors specify the code to execute whenever we create a new instance.
- Finalizers (destructors) – To destroy instances of the class.
Besides the above-mentioned, there are certain other members which are not accessible, the access modifiers determine the accessibility of the members.
The different access modifiers are
- private -Only the particular class and its member function can access private variables and are not accessible outside the class.
- protected – The protected members are accessible in the derived class.
- internal – Internal members are accessible only within files in the same assembly.
- public – public members are accessible everywhere within an application.
Single level Inheritance
It includes one base class and one child or derived class.
Syntax:
<access-specifier> class <base_class>
{
….
}
<access-specifier> class <derived_class>:<base_class>
{
…
}
Example 1:
Explanation:
In the above example, the LocationProperties class is the base class, and the class State is the derived class.
The Location properties class includes data about population and area whereas the State class includes data about Name and NDistricts.
Once the State class inherits the properties of LocationProperties class, its members can be accessed along with its own members by using the object created for the State class.
Thus we can display all the information such as Name, NDistricts, population, and area using the State class object s.
Multi-level inheritance
It includes more than one base and derived classes.
Syntax:
<access-specifier> class first {
….
}
<access-specifier> class second :first
{
…
}
<access-specifier> class third: second
{ …}
class class_name
{
public static void Main(string[] args)
{ ….. }
}
Example 2:
Explanation
This example includes SocialAccounts class, ContactDetail class, and person class. The SocialAccount class is the base class for ContactDetail class, while ContactDetail is the base class for the Person class.
The Person class constructor assigns a value for its own member ‘Name’ and passes all other values such as no, ytid, tid, and insid to ContactDetail class, which in turn assigns value to ‘MobileNo’ and calls the SocialAccounts constructor.
So all the members of SocialAccounts and ContactDetails are accessed using the final Person class object p.