Summary: in this tutorial, you’ll learn about C# inheritance that allows a class to reuse the properties and methods of another class.
Introduction to the C# inheritance
Inheritance is one of the core concepts in object-oriented programming. Inheritance allows a class to inherit from another class.
Inheriting from a class allows you to reuse the functionality of the class instead of building it from scratch.
C# allows a class to inherit only from a single class. This is called single inheritance. C# doesn’t support multiple inheritances where a class can inherit from two or more classes.
Suppose you have a Person
class with properties FirstName
, LastName
, Age
, and FullName
and a method Introduce()
:
// Person.cs
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public byte Age { get; set; }
public string FullName => $"{FirstName} {LastName}";
public string Introduce() => $"Hi, I'm {FullName}.";
}
Code language: C# (cs)
And you want to define an Employee
class that has all the properties as the Person
class and two more properties JobTitle
and Salary
.
Instead of copying code from the Employee
class to the Employee
class, you can use inheritance. The following defines the Employee
class that inherits from the Person
class:
// Employee.cs
class Employee: Person
{
}
Code language: C# (cs)
In this example:
- The
Person
class is called the parent class, a base class, or a super class - The
Employee
class is called a child class, derived class, or subclass.
We also say that the Employee
class extends the Person
class.
Since the Employee
class inherits the properties and methods of the Person
class, its objects behave like objects of the Person
class. For example:
// Program.cs
var employee = new Employee
{
FirstName = "John",
LastName = "Doe",
Age = 25
};
Console.WriteLine(employee.Introduce());
Code language: C# (cs)
Output:
Hi, I'm John Doe.
Code language: C# (cs)
As you can see, the Employee
behaves like the Person
class without duplicating the code. We say that an employee is a person. And the inheritance models an is-a relationship.
Extending a parent class
The following adds the JobTitle
and Salary
properties to the Employee
class:
// Employee.cs
class Employee : Person
{
public string JobTitle { get; set; }
public decimal Salary { get; set; }
}
Code language: C# (cs)
Now, the Employee
class has its own properties and the inherited properties from the Person
class. The following creates a new instance of the Employee
class with all the properties:
// Program.cs
var employee = new Employee
{
FirstName = "John",
LastName = "Doe",
Age = 25,
JobTitle = "C# Developer",
Salary = 120000
};
Console.WriteLine(employee.Introduce());
Code language: C# (cs)
Output:
Hi, I'm John Doe.
Code language: C# (cs)
Summary
- Inheritance allows a class (subclass) to inherit from another class (base class) to extend or customize the base class.
- C# allows a class to inherit from a single class. This is called the single inheritance.