Introduction to Interfaces

Interfaces in C# are a fundamental part of object-oriented programming. They define a contract that classes or structs can implement. An interface specifies what methods, properties, events, or indexers a class or struct must implement, but it does not provide the implementation itself.

Key Concepts

  • Definition: An interface is defined using the interface keyword.
  • Implementation: A class or struct that implements an interface must provide the implementation for all its members.
  • Multiple Inheritance: A class can implement multiple interfaces, providing a way to achieve multiple inheritance in C#.
  • Polymorphism: Interfaces allow for polymorphic behavior, enabling objects to be treated as instances of their interface types.

Syntax

public interface IExample
{
    void Method1();
    int Property1 { get; set; }
}

Example

Let's create an interface IShape and implement it in two classes, Circle and Rectangle.

Interface Definition

public interface IShape
{
    double Area();
    double Perimeter();
}

Implementing the Interface

public class Circle : IShape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public double Area()
    {
        return Math.PI * Radius * Radius;
    }

    public double Perimeter()
    {
        return 2 * Math.PI * Radius;
    }
}

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

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public double Area()
    {
        return Width * Height;
    }

    public double Perimeter()
    {
        return 2 * (Width + Height);
    }
}

Using the Interface

public class Program
{
    public static void Main()
    {
        IShape circle = new Circle(5);
        IShape rectangle = new Rectangle(4, 6);

        Console.WriteLine($"Circle Area: {circle.Area()}");
        Console.WriteLine($"Circle Perimeter: {circle.Perimeter()}");

        Console.WriteLine($"Rectangle Area: {rectangle.Area()}");
        Console.WriteLine($"Rectangle Perimeter: {rectangle.Perimeter()}");
    }
}

Explanation

  • Interface Definition: The IShape interface defines two methods: Area and Perimeter.
  • Circle Class: Implements the IShape interface and provides the implementation for Area and Perimeter methods.
  • Rectangle Class: Also implements the IShape interface and provides its own implementation for Area and Perimeter methods.
  • Program Class: Demonstrates how to use the IShape interface to create instances of Circle and Rectangle and call their methods.

Practical Exercises

Exercise 1: Define and Implement an Interface

Task: Define an interface IVehicle with methods StartEngine and StopEngine. Implement this interface in two classes: Car and Bike.

Solution

public interface IVehicle
{
    void StartEngine();
    void StopEngine();
}

public class Car : IVehicle
{
    public void StartEngine()
    {
        Console.WriteLine("Car engine started.");
    }

    public void StopEngine()
    {
        Console.WriteLine("Car engine stopped.");
    }
}

public class Bike : IVehicle
{
    public void StartEngine()
    {
        Console.WriteLine("Bike engine started.");
    }

    public void StopEngine()
    {
        Console.WriteLine("Bike engine stopped.");
    }
}

public class Program
{
    public static void Main()
    {
        IVehicle car = new Car();
        IVehicle bike = new Bike();

        car.StartEngine();
        car.StopEngine();

        bike.StartEngine();
        bike.StopEngine();
    }
}

Exercise 2: Interface with Properties

Task: Define an interface IEmployee with properties Name and Salary. Implement this interface in a class Employee.

Solution

public interface IEmployee
{
    string Name { get; set; }
    double Salary { get; set; }
}

public class Employee : IEmployee
{
    public string Name { get; set; }
    public double Salary { get; set; }

    public Employee(string name, double salary)
    {
        Name = name;
        Salary = salary;
    }
}

public class Program
{
    public static void Main()
    {
        IEmployee employee = new Employee("John Doe", 50000);
        Console.WriteLine($"Employee Name: {employee.Name}");
        Console.WriteLine($"Employee Salary: {employee.Salary}");
    }
}

Common Mistakes and Tips

  • Not Implementing All Members: Ensure that all members of the interface are implemented in the class.
  • Interface Naming Convention: By convention, interface names start with an uppercase 'I'.
  • Multiple Interfaces: A class can implement multiple interfaces, separated by commas.

Summary

In this section, we covered the basics of interfaces in C#. We learned how to define an interface, implement it in classes, and use it to achieve polymorphism. We also provided practical exercises to reinforce the concepts. Understanding interfaces is crucial for designing flexible and maintainable code in C#.

© Copyright 2024. All rights reserved