C# Abstraction (original) (raw)
Last Updated : 15 Jan, 2025
**Data Abstraction is the property by which only the essential details are shown to the user and non-essential details or implementations are hidden from the user. In other words, Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
**Example: Consider a real-life scenario of withdrawing money from an ATM. The user only knows that in the ATM machine first enters the ATM card, then enters the PIN code of the ATM card, and then enters the amount which the user wants to withdraw and at last, they get their money. They don't know about the inner mechanism of the ATM or the implementation of withdrawing money etc. The user just simply knows how to operate the ATM machine, this is called abstraction.
In C# abstraction is achieved with the help of Abstract classes and Interfaces.
1. Using Abstract Classes
An abstract class is declared with the help of an **abstract keyword. In C# not allowed to create objects of abstract class. We create a derived class take properties of the abstract class and use them to achieve abstraction. Furthermore, we can access it from the Instance of the derived class object. There are some important points to remember regarding abstract classes mentioned below:
- We are not allowed to declare the abstract methods outside the abstract class.
- We can not define properties directly to the abstract method.
- The actual task or behaviour of the abstract method is implemented in the derived class.
- We are not allowed to declare an abstract class as a Sealed Class
- We can not declare an abstract class as Static Class.
**Example:
C# `
using System;
namespace DemoAbstraction {
// abstract class
abstract class Shape
{
// abstract method
// No direct access
public abstract int area();
}
// square class inheriting
// the Shape class
class Square : Shape
{
// private data member
private int side;
// method of square class
public Square(int x = 0)
{
side = x;
}
// overriding of the abstract method of Shape
// class using the override keyword
public override int area()
{
Console.Write("Area of Square: ");
return (side * side);
}
}
class Geeks
{
static void Main(string[] args)
{
// creating reference of Shape class
// which refer to Square class instance
Shape sh = new Square(4);
// calling the method
double result = sh.area();
Console.Write("{0}", result);
}
}
}
`
2. Using Interfaces
The interface is declared using the **interface keyword. We can create abstract methods inside the interface similar to abstract classes and declare their functionality in the derived class. From the derived class we can access those properties using the derived class by creating the Instance of the derived class.
**Example:
C# `
using System;
// interface public interface Student { //abstract method void FavSub(); }
// Subclass for functionality public class Geeky : Student { public void FavSub() { Console.WriteLine("My favorite subject is C#"); } }
public class Geeks { public static void Main() { Student obj = new Geeky();
obj.FavSub();
}
}
`
Output
My favorite subject is C#
Abstraction vs Encapsulation
Abstraction and encapsulation are the important features of OOPs. Their functionality is similar and has slight differences which are shown below.
Features | Abstraction | Encapsulation |
---|---|---|
Definition | Hiding the implementation and exposing the essential details only. | Encapsulate the data and information in a single unit and provide access through accessors. |
Implementation | Implements using abstract classes and interface and properties | Implemented using access modifiers in C# |
Purpose | Used to hide the complex functionality and show the user-friendly Interface | Provide security and prevent unauthorized data access. |
Example | A car class has different features like drive(), break () and gear() and we can use it without knowing the implementation. | Get () and set() accessors to provide access controls on data. |
Advantages of Abstraction
- It reduces the complexity of viewing things.
- Avoids code duplication and increases reusability.
- Helps to increase the security of an application or program as only important details are provided to the user.