Syntax
abstract class ClassName {\n public abstract returnType MethodName();\n}Examples
Basic Abstract Class
Defining a base class that mixes concrete and abstract methods.
public abstract class Animal
{
protected string Name;
protected Animal(string name)
{
Name = name;
}
public abstract void MakeSound(); // no body - subclasses must implement
public void Sleep() // concrete method, shared by all subclasses
{
Console.WriteLine($"{Name} is sleeping");
}
}
public class Dog : Animal
{
public Dog(string name) : base(name) { }
public override void MakeSound() => Console.WriteLine($"{Name} says: Woof!");
}
// Animal a = new Animal("Generic"); // Error - can't instantiate abstract class
Dog dog = new Dog("Rex");
dog.MakeSound();
dog.Sleep();Enforcing a Common Structure
Multiple subclasses implementing the same abstract method differently.
public abstract class Employee
{
protected string Name;
protected double BaseSalary;
protected Employee(string name, double baseSalary)
{
Name = name;
BaseSalary = baseSalary;
}
public abstract double CalculateBonus();
public double TotalPay() => BaseSalary + CalculateBonus();
}
public class Manager : Employee
{
public Manager(string name, double baseSalary) : base(name, baseSalary) { }
public override double CalculateBonus() => BaseSalary * 0.2;
}
public class Developer : Employee
{
public Developer(string name, double baseSalary) : base(name, baseSalary) { }
public override double CalculateBonus() => BaseSalary * 0.1;
}
Employee m = new Manager("Fola", 60000);
Console.WriteLine(m.TotalPay()); // 72000Best practices
- Use an abstract class when subclasses share both state (fields) and some common implemented behavior, not just a method signature
- Use an interface instead when unrelated classes just need to share a capability, with no shared fields or implementation
- A class can only extend one abstract class but can implement multiple interfaces - factor that into your design choice
- Make a method abstract only when every subclass genuinely needs its own distinct implementation - otherwise provide a sensible default in the abstract class
At a glance
- Purpose
- Applications on the .NET platform
- File extension
- .cs
- Runs in
- .NET runtime
- Usually used with
- .NET SDK and libraries
Specifications & further reading
Related C# documentation
Classes & Objects
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.