How Abstract works in C#

How Abstraction Works in C#

In C#, an abstract class is a class that cannot be instantiated directly. It is used as a blueprint for other classes. An abstract method has no body and must be implemented by any non-abstract derived class.

Key Points:

Abstract Class: Cannot be instantiated directly and can have both abstract and non-abstract methods.

Abstract Method: Declared in an abstract class without a body. Derived classes must provide an implementation for this method.


Example: Abstraction with Inheritance in C#

// Abstract class
public abstract class Shape
{
    // Abstract method (no implementation here)
    public abstract double CalculateArea();
    
    // Non-abstract method
    public void DisplayInfo()
    {
        Console.WriteLine("This is a shape.");
    }
}

// Derived class for Circle
public class Circle : Shape
{
    public double Radius { get; set; }

    // Constructor to set radius
    public Circle(double radius)
    {
        Radius = radius;
    }

    // Overriding abstract method
    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }
}

// Derived class for Rectangle
public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    // Constructor to set width and height
    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    // Overriding abstract method
    public override double CalculateArea()
    {
        return Width * Height;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Using derived classes
        Shape circle = new Circle(5);
        Console.WriteLine($"Area of Circle: {circle.CalculateArea()}");

        Shape rectangle = new Rectangle(10, 5);
        Console.WriteLine($"Area of Rectangle: {rectangle.CalculateArea()}");
    }
}

Explanation:

1. Abstract Class (Shape): The class defines a general concept of a "shape" with an abstract method CalculateArea() that derived classes must implement.


2. Derived Classes (Circle and Rectangle): Each class inherits from Shape and provides specific implementations for the CalculateArea() method.


3. Polymorphism: By using the Shape type, we can work with different shapes (circle, rectangle) using a common interface.



Benefits of Abstraction:

Code Reusability: Common methods (like DisplayInfo) can be defined once in the abstract class and reused across multiple derived classes.

Enforcing a Contract: All derived classes must implement the abstract methods, ensuring consistency in how those methods are used.

Hiding Implementation Details: The abstract class provides a common interface, hiding the specific implementations of each derived class.


This way, you achieve abstraction using classes and inheritance, allowing you to define a clear and consistent interface for your classes while leaving the specific implementation details to the subclasses.
#csharp #abstraction #dotnetechpro #dotnet

Comments

Popular posts from this blog

Finding 2nd and 3rd Highest and Lowest Salaries Using LINQ

Sonar qube for code scan

Generate 100 records using linq c#