Abstraction

OOP: Abstraction

Concept of Abstraction:

Abstraction is a fundamental principle of object-oriented programming (OOP) that involves hiding the complex implementation details of an object and exposing only the necessary functionalities or behaviors. It allows developers to focus on what an object does rather than how it does it, thus simplifying the design and maintenance of software systems.

As per provided syntax on the OOP page, abstraction is demonstrated through the use of abstract classes and interfaces.

1. Abstract Class:

An abstract class is a class that cannot be instantiated directly and may contain one or more abstract methods. Abstract methods are declared without implementation and must be implemented by derived classes. In the given program, the 'Shape' class is declared as abstract with an abstract method 'Draw()'. This means that any class inheriting from 'Shape' must provide an implementation for the 'Draw()' method.

abstract class Shape {
    public abstract void Draw();
}

2. Concrete Class (Circle):

A concrete class is a class that provides implementations for all abstract methods inherited from its base abstract class or interface. The 'Circle' class inherits from the 'Shape' abstract class and provides an implementation for the 'Draw()' method by overriding it.

class Circle : Shape {
    public override void Draw() {
        Console.WriteLine("Drawing a circle...");
    }
}

3. Interface (IShape):

An interface is similar to an abstract class, but it cannot contain any implementation. It only defines method signatures that must be implemented by classes that implement the interface. In the given program, the 'IShape' interface declares a method 'Draw()'. Any class that implements 'IShape' must provide an implementation for the 'Draw()' method.

interface IShape {
    void Draw();
}

4. Concrete Class (Rectangle):

The 'Rectangle' class implements the 'IShape' interface by providing an implementation for the 'Draw()' method.

class Rectangle : IShape {
    public void Draw() {
        Console.WriteLine("Drawing a rectangle...");
    }
}

Let's create a complete program:

cs Copy Code
using System;

// Abstract class Shape
abstract class Shape {
    // Abstract method Draw
    public abstract void Draw();
}

// Concrete class Circle inheriting from Shape
class Circle : Shape {
    // Implementation of Draw method for Circle
    public override void Draw() {
        Console.WriteLine("Drawing a circle...");
    }
}

// Interface IShape
interface IShape {
    void Draw();
}

// Concrete class Rectangle implementing IShape
class Rectangle : IShape {
    // Implementation of Draw method for Rectangle
    public void Draw() {
        Console.WriteLine("Drawing a rectangle...");
    }
}

class Program {
    static void Main(string[] args) {
        // Creating objects of Circle and Rectangle
        Circle circle = new Circle();
        Rectangle rectangle = new Rectangle();

        // Calling Draw method for Circle and Rectangle
        circle.Draw();
        rectangle.Draw();

        // Note: Since Shape is abstract, it cannot be instantiated directly.
        // Shape shape = new Shape(); // This will give compilation error.
    }
}
Output:
Drawing a circle...
Drawing a rectangle...

In the 'Main()' method, objects of 'Circle' and 'Rectangle' are created, and their 'Draw()' methods are called to demonstrate polymorphic behavior based on their respective implementations.

In summary, abstraction in C# allows us to define common behaviors or functionalities without specifying how they are implemented. This enhances code flexibility, reusability, and maintainability by allowing us to work at a higher level of abstraction without worrying about low-level implementation details.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.