How to Implement Inheritance in C
Inheritance is a fundamental concept in object-oriented programming
that allows a class to inherit fields, properties, and methods from
another class. This guide explains how to implement inheritance in C#
using the colon (:) syntax, how to pass data to base
constructors, how to override methods using virtual and
override, and how to restrict inheritance using the
sealed keyword.
Basic Inheritance Syntax
In C#, inheritance is established by placing a colon (:)
after the derived class name, followed by the base class name. C#
supports single class inheritance, meaning a class can inherit from only
one base class.
// Base Class (Parent)
public class Vehicle
{
public string Brand { get; set; }
public void StartEngine()
{
Console.WriteLine("The engine is running.");
}
}
// Derived Class (Child)
public class Car : Vehicle
{
public int NumberOfDoors { get; set; }
public void Honk()
{
Console.WriteLine("Beep beep!");
}
}An instance of Car automatically has access to both
Brand and StartEngine() from
Vehicle, as well as its own members.
Car myCar = new Car();
myCar.Brand = "Toyota";
myCar.StartEngine(); // Inherited method
myCar.Honk(); // Derived class methodCalling Base Class Constructors
When a derived class object is instantiated, the base class
constructor executes first. If the base class requires constructor
arguments, use the base keyword to pass values from the
derived constructor to the base constructor.
public class Animal
{
public string Name { get; set; }
public Animal(string name)
{
Name = name;
}
}
public class Dog : Animal
{
public string Breed { get; set; }
public Dog(string name, string breed) : base(name)
{
Breed = breed;
}
}Overriding Base Class Methods
To modify the behavior of a base class method in a derived class:
- Mark the base class method with the
virtualkeyword. - Mark the derived class method with the
overridekeyword.
public class Employee
{
public virtual void Work()
{
Console.WriteLine("Employee is working on general tasks.");
}
}
public class Developer : Employee
{
public override void Work()
{
Console.WriteLine("Developer is writing and debugging code.");
}
}If you need to call the original implementation from inside the
overridden method, call base.MethodName().
Access Modifiers in Inheritance
Access modifiers control which members are inherited:
public: Accessible by any code, including derived classes.protected: Accessible only within the base class and any classes that derive from it.private: Accessible only within the declaring class. Derived classes do not have direct access to private members.internal: Accessible only within the same assembly.
Preventing Inheritance with Sealed Classes
To prevent other classes from inheriting from a class, apply the
sealed modifier.
public sealed class SecurityToken
{
public string TokenId { get; set; }
}
// This will cause a compile-time error:
// public class AdminToken : SecurityToken { }You can also use sealed on overridden methods to prevent
further derived classes from overriding that specific method.