What is abstraction in C#?

Abstraction is a fundamental concept in object-oriented programming that focuses on hiding complex implementation details while showing only the essential features of an object. In C#, abstraction allows you to define what an object does without specifying how it does it.

Abstraction and encapsulation work together − abstraction defines the interface and essential behavior, while encapsulation hides the internal implementation details from the outside world.

Key Concepts of Abstraction

  • Hide complexity − Users interact with simplified interfaces without knowing internal workings

  • Focus on essential features − Only relevant methods and properties are exposed

  • Provide common interface − Multiple classes can share the same abstract structure

Abstraction Concept Abstract Interface (What the user sees) area() display() getName() Hidden Implementation Details Complex calculations, data structures, algorithms

Achieving Abstraction with Abstract Classes

In C#, abstraction is primarily achieved using abstract classes and interfaces. An abstract class provides a partial implementation that derived classes must complete.

Key Rules for Abstract Classes

  • You cannot create an instance of an abstract class

  • You cannot declare an abstract method outside an abstract class

  • When a class is declared sealed, it cannot be inherited; abstract classes cannot be declared sealed

  • Abstract methods must be overridden in derived classes

Syntax

public abstract class ClassName {
    public abstract ReturnType MethodName();
    
    public void ConcreteMethod() {
        // Non-abstract method with implementation
    }
}

Example

using System;

abstract class Shape {
    public abstract int area();
    
    public void DisplayInfo() {
        Console.WriteLine("This is a shape");
    }
}

class Rectangle : Shape {
    private int length;
    private int width;

    public Rectangle(int a = 0, int b = 0) {
        length = a;
        width = b;
    }
    
    public override int area() {
        Console.WriteLine("Rectangle class area:");
        return (width * length);
    }
}

class Circle : Shape {
    private int radius;
    
    public Circle(int r = 0) {
        radius = r;
    }
    
    public override int area() {
        Console.WriteLine("Circle class area:");
        return (int)(3.14 * radius * radius);
    }
}

class Program {
    static void Main(string[] args) {
        Rectangle r = new Rectangle(20, 15);
        Circle c = new Circle(7);
        
        int rectArea = r.area();
        Console.WriteLine("Area: {0}", rectArea);
        
        int circleArea = c.area();
        Console.WriteLine("Area: {0}", circleArea);
        
        r.DisplayInfo();
    }
}

The output of the above code is −

Rectangle class area:
Area: 300
Circle class area:
Area: 153
This is a shape

Benefits of Abstraction

  • Code reusability − Common functionality can be defined once in the abstract class

  • Maintainability − Changes to implementation don't affect the interface

  • Flexibility − Different classes can implement the same abstract methods differently

  • Security − Internal implementation details are hidden from users

Conclusion

Abstraction in C# allows you to define essential features while hiding implementation complexity. Through abstract classes and methods, you can create flexible, maintainable code that provides a clear interface for users while keeping internal details secure and modifiable.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements