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.

Output:

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.

MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More