C# Abstraction - Complete Guide
Introduction to Abstraction
Abstraction in C# is an essential object-oriented programming principle that emphasizes exposing only the necessary behavior of an object while hiding its internal implementation details. It allows developers to focus on what an object does rather than how it does it, making programs easier to design, maintain, and scale.
C# achieves abstraction mainly through abstract classes and interfaces. Abstract classes can include both fully implemented methods and abstract members (without implementation), whereas interfaces define only method and property signatures that must be implemented by any class that inherits them.
Implementing Abstraction
In practice, abstraction can be applied by designing systems that use abstract classes or interfaces as contracts. Concrete classes then provide the specific implementation details. Below are some C# examples showing interfaces, abstract classes, and classes that implement them.
using System;
namespace AbstractionExample
{
// Interface (pure abstraction)
public interface IAnimal
{
string Name { get; set; }
void MakeSound();
void Eat();
void Sleep();
}
// Abstract class (partial abstraction)
public abstract class Vehicle
{
public abstract void Start();
public abstract void Stop();
public void DisplayInfo()
{
Console.WriteLine($"Vehicle Type: {GetType().Name}");
}
public virtual void Maintenance()
{
Console.WriteLine("Performing general vehicle maintenance");
}
public abstract int WheelCount { get; }
}
public interface IElectricVehicle
{
void ChargeBattery();
int BatteryLevel { get; set; }
}
public class Dog : IAnimal
{
public string Name { get; set; }
public Dog(string name) => Name = name;
public void MakeSound() => Console.WriteLine($"{Name} says: Woof! Woof!");
public void Eat() => Console.WriteLine($"{Name} is eating dog food");
public void Sleep() => Console.WriteLine($"{Name} is sleeping");
}
public class Car : Vehicle, IElectricVehicle
{
public override void Start() => Console.WriteLine("Car started with key ignition");
public override void Stop() => Console.WriteLine("Car stopped");
public override int WheelCount => 4;
public int BatteryLevel { get; set; }
public void ChargeBattery()
{
Console.WriteLine("Charging car battery");
BatteryLevel = 100;
}
public override void Maintenance()
{
base.Maintenance();
Console.WriteLine("Changing car oil and checking brakes");
}
public void Drive() => Console.WriteLine("Car is driving on the road");
}
public class Bicycle : Vehicle
{
public override void Start() => Console.WriteLine("Bicycle started by pedaling");
public override void Stop() => Console.WriteLine("Bicycle stopped by braking");
public override int WheelCount => 2;
public override void Maintenance() => Console.WriteLine("Checking bicycle tires and chain");
public void RingBell() => Console.WriteLine("Ring! Ring!");
}
public class RobotDog : IAnimal, IElectricVehicle
{
public string Name { get; set; }
public int BatteryLevel { get; set; }
public RobotDog(string name)
{
Name = name;
BatteryLevel = 50;
}
public void MakeSound() => Console.WriteLine($"{Name} says: Beep! Beep!");
public void Eat()
{
Console.WriteLine($"{Name} is charging instead of eating");
ChargeBattery();
}
public void Sleep() => Console.WriteLine($"{Name} is in low-power mode");
public void ChargeBattery()
{
Console.WriteLine("Charging robot dog battery");
BatteryLevel = 100;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Interface Abstraction ===");
IAnimal dog = new Dog("Buddy");
dog.MakeSound();
dog.Eat();
dog.Sleep();
Console.WriteLine("\n=== Abstract Class Abstraction ===");
Vehicle car = new Car();
Vehicle bicycle = new Bicycle();
car.Start();
car.DisplayInfo();
Console.WriteLine($"Wheel count: {car.WheelCount}");
car.Maintenance();
car.Stop();
Console.WriteLine();
bicycle.Start();
bicycle.DisplayInfo();
Console.WriteLine($"Wheel count: {bicycle.WheelCount}");
bicycle.Maintenance();
bicycle.Stop();
Console.WriteLine("\n=== Multiple Interface Implementation ===");
RobotDog robotDog = new RobotDog("Robo");
robotDog.MakeSound();
Console.WriteLine($"Battery level: {robotDog.BatteryLevel}%");
robotDog.Eat();
Console.WriteLine($"Battery level after charging: {robotDog.BatteryLevel}%");
Console.WriteLine("\n=== Polymorphism with Abstraction ===");
IAnimal[] animals = { dog, robotDog };
Vehicle[] vehicles = { car, bicycle };
Console.WriteLine("\nAnimals:");
foreach (var animal in animals)
animal.MakeSound();
Console.WriteLine("\nVehicles:");
foreach (var vehicle in vehicles)
{
vehicle.Start();
vehicle.Stop();
Console.WriteLine();
}
Console.WriteLine("=== Type Casting with Interfaces ===");
if (robotDog is IElectricVehicle electric)
{
Console.WriteLine("Robot dog is an electric vehicle");
electric.ChargeBattery();
}
}
}
}
=== Interface Abstraction === Buddy says: Woof! Woof! Buddy is eating dog food Buddy is sleeping === Abstract Class Abstraction === Car started with key ignition Vehicle Type: Car Wheel count: 4 Performing general vehicle maintenance Changing car oil and checking brakes Car stopped Bicycle started by pedaling Vehicle Type: Bicycle Wheel count: 2 Checking bicycle tires and chain Bicycle stopped by braking === Multiple Interface Implementation === Robo says: Beep! Beep! Battery level: 50% Robo is charging instead of eating Charging robot dog battery Battery level after charging: 100% === Polymorphism with Abstraction === Animals: Buddy says: Woof! Woof! Robo says: Beep! Beep! Vehicles: Car started with key ignition Car stopped Bicycle started by pedaling Bicycle stopped by braking === Type Casting with Interfaces === Robot dog is an electric vehicle Charging robot dog battery