Inheritance in C# is an object oriented programming feature that allows one class to reuse and extend the members of another class. The class that is inherited from is called the base class, and the class that inherits is called the derived class.
Inheritance helps when multiple classes share common data or behavior. Instead of repeating the same code in every class, common logic can be placed in a base class. Derived classes can then reuse that logic and add their own specialized behavior.
Used correctly, inheritance improves code reuse and supports polymorphism. Used carelessly, it can create tightly coupled classes that are difficult to change. That is why it is important to understand not only the syntax but also when inheritance is the right design choice.
What Is Inheritance in C#?
Inheritance allows a class to acquire members from another class. The derived class can use accessible fields, properties, and methods from the base class. It can also add new members or override virtual members.
class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine("Animal is eating");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Dog is barking");
}
}
Here, Dog inherits from Animal. A Dog object can use Name and Eat from the base class, and it can also use its own Bark method.
Dog dog = new Dog();
dog.Name = "Buddy";
dog.Eat();
dog.Bark();
Base Class and Derived Class
The base class contains common members. The derived class inherits those members and can add more specific behavior. In inheritance, the derived class represents a more specialized version of the base class.
| Term | Meaning | Example |
|---|---|---|
| Base class | The class being inherited from | Animal |
| Derived class | The class that inherits from base class | Dog |
| Inherited member | Accessible member reused by derived class | Eat() |
| Specialized member | Member added by derived class | Bark() |
The relationship should make sense. A dog is an animal, so inheritance is reasonable. But a car is not an engine, so inheritance between Car and Engine would be a poor design. That relationship is better represented using composition.
Syntax of Inheritance in C#
class DerivedClass : BaseClass
{
// additional members
}
The colon symbol is used to inherit from a base class. C# supports single class inheritance, which means a class can inherit from only one class directly.
Single Inheritance in C#
Single inheritance means one derived class inherits from one base class. This is the most common inheritance pattern in C#.
class Vehicle
{
public void Start()
{
Console.WriteLine("Vehicle started");
}
}
class Car : Vehicle
{
public void Drive()
{
Console.WriteLine("Car is driving");
}
}
The Car class inherits Start from Vehicle and adds its own Drive method.
Multilevel Inheritance in C#
Multilevel inheritance happens when a class inherits from a derived class, creating a chain of inheritance. C# supports this because each class still directly inherits from only one class.
class Animal
{
public void Eat()
{
Console.WriteLine("Eating");
}
}
class Mammal : Animal
{
public void Walk()
{
Console.WriteLine("Walking");
}
}
class Dog : Mammal
{
public void Bark()
{
Console.WriteLine("Barking");
}
}
A Dog object can use members from Dog, Mammal, and Animal. Multilevel inheritance should be used carefully. Very deep inheritance chains are harder to understand and debug.
Does C# Support Multiple Class Inheritance?
C# does not support multiple inheritance with classes. A class cannot inherit from two classes directly. This avoids ambiguity problems that can happen when two base classes contain members with the same name or conflicting behavior.
// Not allowed in C#
class SmartPhone : Camera, MusicPlayer
{
}
Instead, C# supports implementing multiple interfaces. A class can inherit from one class and implement many interfaces. Interfaces are covered separately, but this is the standard way to represent multiple capabilities in C#.
class SmartPhone : Device, ICamera, IMusicPlayer
{
}
Access Modifiers and Inheritance
Not every member of a base class is accessible in a derived class. Access modifiers control what is inherited and what can be used directly.
| Modifier | Access from Derived Class |
|---|---|
public | Accessible |
protected | Accessible inside derived class |
private | Not directly accessible |
internal | Accessible only in same assembly |
A private member still exists as part of the base class object, but the derived class cannot access it directly. If derived classes need controlled access, use protected members or public/protected methods.
base Keyword in C#
The base keyword is used to access members of the base class from inside a derived class. It is commonly used to call a base class constructor or to call an overridden base method.
class Person
{
public string Name { get; }
public Person(string name)
{
Name = name;
}
}
class Employee : Person
{
public int EmployeeId { get; }
public Employee(string name, int employeeId) : base(name)
{
EmployeeId = employeeId;
}
}
The Employee constructor calls the Person constructor using base(name). This makes sure the base part of the object is initialized properly.
Method Overriding in Inheritance
A derived class can provide its own version of a base class method if the base method is marked with virtual. The derived class uses override to replace the behavior.
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing circle");
}
}
When Draw is called on a Circle object, the overridden method runs. This is one of the foundations of runtime polymorphism in C#.
Calling Base Method from Override
Sometimes the derived class wants to add behavior but still keep the base class logic. In that case, the overridden method can call base.MethodName().
class Logger
{
public virtual void Log(string message)
{
Console.WriteLine(message);
}
}
class TimestampLogger : Logger
{
public override void Log(string message)
{
base.Log($"{DateTime.Now}: {message}");
}
}
This example keeps the main logging behavior in the base class while changing the message format in the derived class. This is useful when a derived class should extend behavior instead of completely replacing it.
Constructors in Inheritance
Constructors are not inherited in the same way methods are inherited. When a derived object is created, the base class constructor runs first, then the derived class constructor runs. This ensures the base part of the object is initialized before the derived part.
class BaseClass
{
public BaseClass()
{
Console.WriteLine("Base constructor");
}
}
class DerivedClass : BaseClass
{
public DerivedClass()
{
Console.WriteLine("Derived constructor");
}
}
DerivedClass obj = new DerivedClass();
The output shows that the base constructor runs before the derived constructor. If the base class requires parameters, the derived class must call the correct base constructor using the base keyword.
sealed Class in C#
A sealed class cannot be inherited. Use sealed when a class should not be used as a base class, either for design, security, or correctness reasons.
sealed class FinalReport
{
public void Print()
{
Console.WriteLine("Printing final report");
}
}
// Not allowed
// class CustomReport : FinalReport { }
Sealing a class communicates that the type is complete and not designed for extension. This can prevent incorrect inheritance and protect important assumptions inside the class.
Inheritance vs Composition
Inheritance represents an is-a relationship. Composition represents a has-a relationship. This distinction is important for clean design.
| Relationship | Use | Example |
|---|---|---|
| Inheritance | When one type is a specialized version of another | Dog is an Animal |
| Composition | When one type contains another type | Car has an Engine |
Many design problems happen when inheritance is used for a relationship that should be composition. If the sentence “A is a B” sounds wrong, inheritance is probably wrong too.
Common Mistakes with Inheritance
- Using inheritance only to reuse a few methods.
- Creating very deep inheritance chains.
- Making base classes too large and too general.
- Using inheritance when composition would be clearer.
- Making too many members protected without a clear extension plan.
- Overriding methods in a way that breaks expected base class behavior.
A common beginner mistake is thinking inheritance is always good because it reduces duplicate code. Code reuse is useful, but inheritance also creates a strong relationship between classes. If the base class changes, derived classes may be affected.
Best Practices for Inheritance in C#
- Use inheritance only when the relationship is truly an is-a relationship.
- Keep base classes focused and stable.
- Prefer composition when a class only needs to use another class.
- Use
virtualandoverrideintentionally. - Avoid deep inheritance hierarchies.
- Use interfaces for capabilities that many unrelated classes can share.
- Seal classes that are not designed to be inherited.
Inheritance Interview Points
For interviews, remember that C# supports single class inheritance but does not support multiple class inheritance. A class can inherit from one class and implement multiple interfaces. The base keyword is used to access base class constructors and members. The virtual and override keywords are used for method overriding.
Also remember that private members are not directly accessible in derived classes, constructors are not inherited like normal methods, and the base class constructor runs before the derived class constructor.
Design Rule for Inheritance
Use inheritance when the derived class can safely stand in for the base class without surprising behavior. If a derived class must constantly disable, ignore, or rewrite base class behavior, the hierarchy is probably wrong and composition will usually be cleaner.
That keeps future changes safer and predictable.
FAQs on Inheritance in C#
Does C# support multiple inheritance?
C# does not support multiple inheritance with classes. A class can inherit from one class, but it can implement multiple interfaces.
What is the base keyword in C#?
The base keyword is used to access members or constructors of the base class from inside a derived class.
Can private members be inherited in C#?
Private members exist as part of the base class object, but they are not directly accessible from the derived class.
When should I avoid inheritance?
Avoid inheritance when the relationship is not truly is-a, when the hierarchy becomes deep, or when composition would make the design simpler.