Interfaces and abstract classes in C#

In C#, interfaces and abstract classes are two ways to define contracts or blueprints for classes. They help to define a set of rules or guidelines that a class must follow, which makes it easier to write code that is flexible and maintainable.

Here are some of the key concepts related to interfaces and abstract classes in C#:

1. Interfaces: An interface is a contract that defines a set of methods, properties, and events that a class must implement. An interface can be thought of as a blueprint for a set of related classes that share a common set of behaviors.

Here is an example of an interface in C#:

public interface IShape
{
    double Area { get; }
    void Draw();
}

This code defines an interface named “IShape” with a read-only property “Area” and a void method “Draw”. Any class that implements the “IShape” interface must provide an implementation for the “Area” and “Draw” members.

2. Abstract classes: An abstract class is a class that cannot be instantiated directly and is intended to be used as a base class for derived classes. An abstract class can contain abstract and non-abstract members, and any derived class must implement the abstract members.

Here is an example of an abstract class in C#:

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

    public void Eat()
    {
        Console.WriteLine("The animal is eating");
    }
}

This codedefines an abstract class named “Animal” with an abstract method “MakeSound” and a non-abstract method “Eat”. Any derived class that inherits from “Animal” must implement “MakeSound”, but can also use the “Eat” method without modification.

3. Differences between interfaces and abstract classes: While interfaces and abstract classes both define contracts for classes to follow, there are some key differences between them. One major difference is that a class can implement multiple interfaces, but can only inherit from one abstract class. Another difference is that interfaces can only define method signatures, while abstract classes can also define properties, fields, and non-abstract methods.

Here is an example that illustrates the differences between interfaces and abstract classes:

public interface IResizable
{
    void Resize(double factor);
}

public abstract class Shape
{
    public abstract double Area { get; }
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

public class Rectangle : Shape, IResizable
{
    public double Width { get; set; }
    public double Height { get; set; }

    public override double Area => Width * Height;

    public void Resize(double factor)
    {
        Width *= factor;
        Height *= factor;
    }

    public override void Draw()
    {
        Console.WriteLine("Drawing a rectangle");
    }
}

This code defines an interface named “IResizable” with a method named “Resize”, an abstract class named “Shape” withan abstract property named “Area” and a virtual method named “Draw”, and a class named “Rectangle” that inherits from “Shape” and implements “IResizable”. The “Rectangle” class must provide an implementation for “Area”, “Resize” and “Draw”, and it can override the “Draw” method of the base class to provide a more specific implementation.

Interfaces and abstract classes are both powerful tools in C# for defining contracts and creating flexible and maintainable code. By using interfaces and abstract classes, you can write code that is easy to understand, extend, and reuse.