Interface in C#

Mannan Ul Haq
0
Interfaces in C# provide another way to achieve abstraction. An interface is like a completely "abstract class" because it only contains abstract methods and properties (without any implementation). It is a blueprint or contract for classes to follow. It defines a set of methods, properties, or events that any class implementing the interface must provide.

Example:

interface IShape
{
    void Draw(); // Interface method for drawing a shape
    double CalculateArea(); // Interface method for calculating the shape's area
}

In this example, the `IShape` interface declares two abstract methods: `Draw()` and `CalculateArea()`. Any class that implements this interface must provide concrete implementations for these two methods.

It's a good practice to name interfaces with an "I" at the beginning, making it easier to distinguish them from regular classes.
NOTE: By default, the members of an interface are abstract and public. (alert-success)

To implement an interface, a class needs to use the `: operator` and mention the interface name after its class name:

Example:

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

    public double CalculateArea()
    {
        // Implementation for calculating the area of a circle
        return Math.PI * radius * radius;
    }

    // Other properties and methods specific to the Circle class
    private double radius;
}

Here, the `Circle` class implements the `IShape` interface. It provides the necessary implementation for the `Draw()` and `CalculateArea()` methods. The interface ensures that any class implementing it adheres to these method signatures.
NOTE: Unlike regular methods, there's no need to use the "override" keyword when implementing interface methods. (alert-success)

Unlike C++, C# does not support multiple or hybrid inheritance for classes, but it does allow the implementation of multiple interfaces and a form of hybrid inheritance through interfaces.

Multiple Inheritance with Interface:

Multiple inheritance allows a derived class to inherit from multiple base interfaces. This means that the derived class can combine the features and behaviors of multiple base interfaces.

Example:


interface IBase1
{
    // Base interface 1 members
}

interface IBase2
{
    // Base interface 2 members
}

class Derived : IBase1, IBase2
{
    // Derived class members
}


Hybrid Inheritance with Interface:

Hybrid inheritance combines multiple types of inheritance. It is a combination of single, multiple, and multilevel inheritances. Hybrid inheritance allows for great flexibility in class design, but it can also become complex and harder to manage.

Example:


interface IBase1
{
    // Base1 interface members
}

interface IBase2
{
    // Base2 interface members
}

class Derived1 : IBase1
{
    // Derived class 1 members
}

class Derived3 : Derived1
{
    // Derived class 3 members
}

class Derived4 : Derived1
{
    // Derived class 4 members
}

class Derived2 : IBase1, IBase2
{
    // Derived class 2 members
}


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !