Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

C# Abstraction:


Abstraction in C# is a concept that allows you to define a blueprint for a class with some methods and properties without implementing the details of those methods. It focuses on what an object does rather than how it does it. Abstraction hides the complexity of the implementation and only shows the essential features of an object.

Here's a simple explanation with an example:

Suppose we want to create a basic shape interface called IShape with a method Draw() that every shape must implement. The actual implementation of the Draw() method will vary depending on the specific shape.

Example

using System;

// Interface defining the blueprint for shapes
interface IShape
{
void Draw(); // Method to draw the shape
}

// Concrete class implementing the IShape interface
class Circle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a circle");
}
}

// Concrete class implementing the IShape interface
class Rectangle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}

class Program
{
static void Main(string[] args)
{
IShape shape1 = new Circle(); // Creating a circle
IShape shape2 = new Rectangle(); // Creating a rectangle

shape1.Draw(); // Calls the Draw() method of Circle class
shape2.Draw(); // Calls the Draw() method of Rectangle class
}
}

In this example:

Abstraction helps in managing complexity by focusing on essential features and hiding unnecessary details. It also promotes code reusability and modularity.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java