C# Abstraction

Last Updated : 13 Oct, 2026

Abstraction in C# is an object-oriented programming (OOP) principle that hides implementation details and exposes only the essential functionality of an object. It focuses on what an object does rather than how it performs the operation.

  • Reduces complexity by exposing only the necessary operations to users.
  • Achieved primarily using abstract classes and interfaces.
  • Allows implementations to change without affecting code that depends on the abstraction, provided the contract remains unchanged.

Real-Life Example of Abstraction

Consider a vehicle. A driver can start a vehicle without needing to understand the internal mechanism of its engine. Different vehicles may use different mechanisms, but the operation remains the same from the driver's perspective.

C#
using System;

abstract class Vehicle{
    
    // Abstract method
    public abstract void Start();
}

// Derived class
class Car : Vehicle {
    public override void Start()
    {
        Console.WriteLine(
            "Car starts with a key or button.");
    }
}

class Program {
    static void Main()
    {
        Vehicle vehicle = new Car();
        vehicle.Start();
    }
}

Output
Car starts with a key or button.

Explanation:

  • Vehicle is an abstract class that declares the abstract method Start().
  • Car inherits from Vehicle and provides the implementation of Start().
  • Vehicle vehicle = new Car(); allows the program to use the Vehicle abstraction while the actual implementation is provided by Car.

Ways to Achieve Abstraction

C# provides two primary ways to achieve abstraction:

abstraction


  • Abstract Classes: Define common functionality and allow derived classes to implement specific behavior.
  • Interfaces: Define contracts that different classes or structures can implement.

Both allow developers to work with common operations without depending on their internal implementations.

1. Abstraction Using Abstract Classes

An abstract class in C# is declared using the abstract keyword and cannot be instantiated directly. It can contain abstract members without implementations and concrete members with implementations, allowing derived classes to share common behavior while providing their own specific functionality.

  • Can contain both abstract and concrete methods.
  • Can define fields, properties, constructors, and other members.
  • A non-abstract derived class must implement all inherited abstract members.

Example: Abstraction Using an Abstract Class

The following program demonstrates abstraction using a Shape class with an abstract method Area().

C#
using System;

abstract class Shape{
    
    public abstract void Area();
}

class Circle : Shape {
    public override void Area()
    {
        Console.WriteLine("Area of Circle: "
                          + (3.14 * 5 * 5));
    }
}

class Geeks{
    static void Main()
    {
        Shape shape = new Circle();
        shape.Area();
    }
}

Output
Area of Circle: 78.5

Explanation:

  • Shape defines an abstract method Area() without providing its implementation.
  • Circle inherits from Shape and implements the method using the override keyword.
  • The Main() method accesses Area() through a Shape reference without knowing its calculation logic.

2. Abstraction Using Interfaces

An interface in C# defines a contract that specifies the functionality implementing types must provide. An interface can declare methods, properties, events, and indexers. Modern C# also supports interface members with default implementations.

  • Classes and structures use the : syntax to implement interfaces.
  • A class or structure can implement multiple interfaces.
  • Interface-based abstraction allows different types to expose the same behavior through a common contract.

Example: Abstraction Using an Interface

The following program demonstrates abstraction using an IShape interface.

C#
using System;

interface IShape{
    
    void Area();
}

class Rectangle : IShape{
    
    public void Area()
    {
        Console.WriteLine("Area of Rectangle: " + (4 * 5));
    }
}

class Geeks{
    static void Main()
    {
        IShape shape = new Rectangle();
        shape.Area();
    }
}

Output
My favorite subject is C#

Explanation:

  • IShape defines a contract containing the Area() method.
  • Rectangle implements the interface and provides its own area calculation.
  • The program accesses the method through an IShape reference, without depending on the concrete Rectangle implementation.

Abstract Class Vs Interface

Both abstract classes and interfaces provide abstraction, but they differ in their structure and usage.

Feature

Abstract Class

Interface

Declaration

Uses abstract class

Uses interface

Methods

Supports abstract and concrete methods

Supports abstract and default-implemented methods

Fields

Can contain instance fields

Cannot contain instance fields

Constructors

Can have constructors

Cannot have instance constructors

Inheritance

A class can inherit from one base class

A class can implement multiple interfaces

Main Purpose

Share common state and functionality

Define contracts for different types

Advantages of Abstraction

  • Reduces Complexity: Hides implementation details and exposes essential functionality.
  • Improves Maintainability: Internal implementations can change without affecting dependent code when the contract remains unchanged.
  • Promotes Reusability: Common functionality can be shared through abstract classes.
  • Supports Flexibility: Different classes can provide different implementations of the same operation.
  • Encourages Loose Coupling: Allows code to depend on abstractions rather than specific implementations.

Limitations of Abstraction

  • Introduces additional classes or interfaces that may increase complexity.
  • Poorly designed abstractions can make code difficult to understand.
  • Changes to abstract members or interface contracts may require updates to implementing classes.
  • Excessive use of abstraction can make debugging and maintenance more difficult.
Comment

Explore