Exploring Interface and Abstract Class in C# Programming

Introduction

In C#, both interfaces and abstract classes are powerful tools for designing flexible and reusable code. They enable developers to define contracts that classes must adhere to while providing a structure for organizing and implementing common functionalities. Let's delve into the concepts of interface and abstract class, explore their differences, and see examples of how they are used.

Interface in C#

An interface in C# defines a contract that a class must implement. It consists of method signatures, properties, events, or indexers without any implementation. Interfaces provide a way to achieve multiple inheritance since a class can implement multiple interfaces.

Example

using System;

interface IShape
{
    double GetArea();
    double GetPerimeter();
}

class Rectangle : IShape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public double GetArea()
    {
        return Length * Width;
    }

    public double GetPerimeter()
    {
        return 2 * (Length + Width);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Rectangle rectangle = new Rectangle { Length = 5, Width = 3 };

        Console.WriteLine("Rectangle Area: " + rectangle.GetArea());
        Console.WriteLine("Rectangle Perimeter: " + rectangle.GetPerimeter());
    }
}

Output

Rectangle Area: 15
Rectangle Perimeter: 16

In this example, IShape is an interface defining methods for calculating the area and perimeter of a shape. The Rectangle class implements this interface and provides its own implementation for the methods. In the Main method, we create an instance of Rectangle, set its dimensions, and then call its GetArea() and GetPerimeter() methods to calculate and display the area and perimeter.

Abstract Class in C#

An abstract class in C# is a class that cannot be instantiated on its own and may contain abstract members, i.e., methods or properties without implementation. Abstract classes provide a way to define a common structure and behavior for derived classes.

Example

using System;

abstract class Animal
{
    public abstract void MakeSound();
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal dog = new Dog();
        Animal cat = new Cat();

        dog.MakeSound(); // Output: Woof!
        cat.MakeSound(); // Output: Meow!
    }
}

Output

Woof!
Meow!

In this example, Animal is an abstract class with an abstract method MakeSound(). The Dog and Cat classes derive from Animal and provide their own implementations of the MakeSound() method. In the Main method, we create instances of Dog and Cat and call their MakeSound() methods to produce the respective animal sounds.

Conclusion

Interfaces and abstract classes are essential building blocks of object-oriented programming in C#. While interfaces define contracts that classes must adhere to, abstract classes provide a common structure and behavior for derived classes. Understanding when to use each depends on the specific requirements of your application, but both contribute to writing maintainable, extensible, and reusable code.

😊Please consider liking and following me for more articles and if you find this content helpful.👍


Similar Articles
Citiustech Healthcare Technology Pvt Ltd
CitiusTech plays a deep and meaningful role in powering the future of healthcare.