C# Interface

Interfaces in C#

An interface in C# is a reference type that defines a contract for other classes to implement. It declares a set of methods, properties, events, or indexers that implementing classes must provide. However, it doesn’t contain any implementation details itself; it only defines the signatures of the members.

It is used to achieve multiple inheritance which can’t be achieved by class. It is used to achieve full abstraction because it cannot have a method body.

Basic Structure :

public interface IMyInterface
{
// Declare methods (no implementation)
void Methodname();
}

Interface Declaration:

  • Interfaces are declared using the interface keyword followed by the interface name (IMyInterface in this example).
  • By convention, interface names in C# typically start with an uppercase “I”.

Scenario: Create a program to simulate different pets. Each pet can make its own sound. You will use an interface class for specific behaviours.

using System;
public interface IAnimal
    {
        void MakeSound();
    }
public class Dog : IAnimal
{
public void MakeSound()
     {
        Console.WriteLine(“The dog says: Woof Woof”);
      }
}
public class Cat : IAnimal
{
        public void MakeSound()
      {
       Console.WriteLine(“The cat says: Meow Meow”);
       }
}
class Program
{
         static void Main()
      {
         Dog myDog = new Dog();
         Cat myCat = new Cat();
         myDog.MakeSound();
         myCat.MakeSound();
     }
}

Explanation:

using System;

// Define an interface named IAnimal

public interface IAnimal
{
void MakeSound(); // Method declaration for making a sound
}

// Implement the interface in a class named Dog

public class Dog : IAnimal
  {
// Implement the MakeSound method defined in IAnimal
public void MakeSound()
         {
          Console.WriteLine(“The dog says: Woof Woof”);
         }
  }

// Implement the interface in a class named Cat

public class Cat : IAnimal
   {
// Implement the MakeSound method defined in IAnimal
public void MakeSound()
        {
               Console.WriteLine(“The cat says: Meow Meow”);
        }
    }

// Main program class

class Program
{
         static void Main()
    {
         // Create instances of Dog and Cat using their specific types
            Dog myDog = new Dog();
            Cat myCat = new Cat();
        // Call the MakeSound method on each instance
           myDog.MakeSound(); // Outputs: The dog says: Woof Woof
           myCat.MakeSound(); // Outputs: The cat says: Meow Meow
     }
}

Task:

1. Basic Interface Implementation:
Create an interface IAnimal with a method Speak. Implement this interface in two classes Dog and Cat with their respective implementations of Speak method. In the Main method, create instances of Dog and Cat and call their Speak methods.

2. Interface with Properties and Methods:
Define an interface IVehicle with a property Speed and a method Drive. Implement this interface in two classes Car and Bike. Set the speed and implement the Drive method to print a message indicating the vehicle’s speed. In the Main method, create instances of Car and Bike, set their speeds, and call the Drive method.

3. Interface Inheritance:
Create an interface IShape with methods CalculateArea and CalculatePerimeter. Create another interface IColoredShape that inherits from IShape and adds a property Color. Implement IColoredShape in a class Rectangle. In the Main method, create an instance of Rectangle, set its properties, and call its methods.
.
4. Interface and Abstract Class Combination:
Create an interface IRunnable with a method Run and an abstract class Machine with an abstract method Start. Create a class Robot that inherits from Machine and implements IRunnable. Provide implementations for the Start and Run methods. In the Main method, create an instance of Robot, and call its methods.