C# Inheritance

C# is an Object-oriented programming language. Object-oriented programming has four main concepts that are.

  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

In this section, you will learn about inheritance in C#.

Inheritance allows us to reuse the code. It also makes the code more readable and maintainable.

Let's understand the Inheritance with an example.

In the following Image, there are two classes named FullTimeEmployee.cs and PartTimeEmployee.cs. If you will notice both the classes have almost similar properties except the FullTimeEmployee.cs has MonthalySalary property and PartTimeEmployee.cs has HourlyRate property.

In the above example, there are three properties FirstName, LastName and Phone which are being used in both the classes. It's not a good practice to repeat the same code again because it violates the concept of Inheritance and creates an extra overload of maintaining the code.

The same can be achieved using the concept of Inheritance by creating a new base class. In the following example, EmployeeBase class is containing the common properties, further to access these properties both the classes FullTimeEmployee and PartTimeEmployee are inheriting the EmployeeBase class.

To inherit a class in C# a colon(:) character is used after the child class name.

Following is an example of inheritance.

Example

Try It
public static void Main()
{
    FullTimeEmployee FTE = new FullTimeEmployee();
    FTE.FirstName = "Joe";
    FTE.LastName = "Sen";
    FTE.Phone = "XXXXXXXX78";
    FTE.MonthalySalary = 1000;
    FTE.PrintDetails();
 
    PartTimeEmployee PTE = new PartTimeEmployee();
    PTE.FirstName = "Jhon";
    PTE.LastName = "Doe";
    PTE.Phone = "XXXXXXXX58";
    PTE.HourlyRate = 10;
    PTE.PrintDetails();
 
}

class EmployeeBase
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
}
 
class FullTimeEmployee : EmployeeBase
{
    public float MonthalySalary { get; set; }
 
    public void PrintDetails()
    {
        Console.WriteLine("Employee first name is : " + this.FirstName);
        Console.WriteLine("Employee last name is : " + this.LastName);
        Console.WriteLine("Employee phone is : " + this.Phone);
        Console.WriteLine("Employee monthly salary name is : " + this.MonthalySalary);
    }
}
 
class PartTimeEmployee : EmployeeBase
{
    public float HourlyRate { get; set; }
 
    public void PrintDetails()
    {
        Console.WriteLine("Employee first name is : " + this.FirstName);
        Console.WriteLine("Employee last name is : " + this.LastName);
        Console.WriteLine("Employee phone is : " + this.Phone);
        Console.WriteLine("Employee monthly salary name is : " + this.HourlyRate);
    }
}
Output

Employee first name is : Joe
Employee last name is : Sen
Employee phone is : XXXXXXXX78
Employee monthly salary name is : 1000


Employee first name is : Jhon
Employee last name is : Doe
Employee phone is : XXXXXXXX58
Employee monthly salary name is : 10

It's important to understand that C# supports only single class inheritance, we can not inherit more than one class. However, a C# class can implement multiple interfaces.

In the previous section, you have learned about the differences and similarities between a class and structure. If we will talk about inheritance then Structs do not support inheritance but a structure can implement interfaces.

Let's learn about the interface in the next section.