C# Abstraction2 min read

Abstraction in object-oriented programming is a way to hide the details and only displaying the relevant information to the users. It is a technique through which we can separate the interface with the implementation details.

Abstraction and encapsulation are the related feature in C#. Abstraction is used to hide the implementation detail while encapsulation is a way to achieve the desired level of abstraction.

Take a real-life example:
Suppose you are switching the channel with your TV remote control. You click the button to switch to the desired channel on a TV but how the implementation of switching channel with a click of a button is unknown to you, that is an abstraction. It hides the detail of how the work is done inside the remote control. The users are only familiar with the button and how to operate with the button.

In C#, abstraction can be achieved in two ways.

  • Abstract class.
  • Interfaces.

Let us first see an abstract method.

Abstract Method

Abstraction and interface both have abstract methods. The method that has no body is called the abstract method. An abstract method is also declared with an abstract keyword and ends with a semicolon(;) instead of curly braces({}).

Syntax of abstract method:

public abstract void methodName();

C# Abstract Class

In C#, the class declared with an abstract keyword is called an abstract class. An abstract class may or may not have abstract methods.

An object of abstract class cannot be created in C#, to access this class, it must be inherited from another class.

Syntax of an abstract class:

abstract class ClassName {
. . . . .

Let us go through an example of an abstract class in C#

Example: C# program for an abstract method

In the example below, we create an abstract class with an abstract method that is inherited by another class.


Dog barks
Cat Meows

Advantage of data abstraction

  • It also avoids code duplication increases the reusability of the code.
  • Since the abstraction separates the interface and implementation, the internal changes can be easily made without affecting the user-level code.
  • The abstracting of the data itself increases the security as no user can access the internal hidden mechanism.


Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …