C# Interview Question #16

What is inheritance in C#?

OOP & Core C# Concepts Junior Beginner

Quick Interview Answer

Inheritance allows a derived class to reuse and extend members defined by a base class.

Detailed Explanation

Inheritance allows a derived class to reuse and extend members defined by a base class.

C# supports single class inheritance, meaning a class can directly inherit from only one base class. However, a class can implement multiple interfaces.

Inheritance is useful when types have a genuine 'is-a' relationship, but composition is often preferred when behavior can be assembled without creating a rigid inheritance hierarchy.

Code Example

public class Person
{
    public string Name { get; set; } = string.Empty;
}

public class Employee : Person
{
    public decimal Salary { get; set; }
}