Abstraction in programming refers to the concept of hiding the complex implementation details of a system
or a class and exposing only
the essential features or functionalities to the outside world.
It allows developers to focus on what an object does rather than how it does
it.
Abstraction is achieved by defining interfaces, abstract classes,
and methods
that provide a simplified view of the underlying complexity.
namespace ConsoleApp1
{
// Abstract class representing a geometric figure
public abstract class Figure
{
// Abstract method to calculate area (to be implemented by derived
classes)
public abstract double CalculateArea();
}
// Concrete class representing a circle
public class Circle : Figure
{
// Fields
private double radius;
// Constructor
public Circle(double radius)
{
this.radius = radius;
}
// Implementation of CalculateArea method for a circle
public override double CalculateArea()
{
return Math.PI * radius * radius;
}
}
// Concrete class representing a rectangle
public class Rectangle : Figure
{
// Fields
private double length;
private double width;
// Constructor
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
// Implementation of CalculateArea method for a rectangle
public override double CalculateArea()
{
return length * width;
}
}
class Program
{
static void Main(string[] args)
{
// Create objects of different figures
Figure circle = new Circle(5);
Figure rectangle
= new Rectangle(4,
6);
// Calculate and display areas of figures
Console.WriteLine("Area of circle:
" + circle.CalculateArea());
Console.WriteLine("Area of rectangle:
" +
rectangle.CalculateArea());
}
}
}