C Sharp Interface

Comparable as a Class, Interface can have methods, properties, events, and indexers as its individuals. But interfaces will contain just the declaration of the members. The implementation of the interface’s members will be given by class who implements the interface implicitly or explicitly.

Important points about interface:

  • Interfaces specify what a class must do and not how.
  • Interfaces can’t have private members.
  • By default, all the members of Interface are public and abstract.
  • The interface will always define with the help of keyword ‘interface‘.
  • Interface cannot contain fields because they represent a particular implementation of data.
  • Multiple inheritance is possible with the help of Interfaces but not with classes.

Syntax for Interface:

interface    myInterface
{
        //   we can declare Events
        //   we can declare indexers
        //   we can declare functions/methods      
        //   we can declare properties
}

Syntax for Implementing Interface:

class   Student  :  myInterface

To announce an interface, utilize interface catchphrase. It is have utilized to supply add up to deliberation. Meaning all the individuals within the interface are announced with the purge body and are open and theoretical by default. A course that actualizes interface must execute all the functions/methods announced within the interface.

Example 1:

// working of interface

using System;
 
interface inter1
{
    void display();
}

class student : inter1
{
 
    public void display()
    {
        Console.WriteLine("I am in Code Config Sample program.");
    }
 
    // Main Method
    public static void Main (String []args)
    {
       student t = new student();
        t.display();
    }
}

Output:

I am in Code Config Sample program.

Let’s try with another example below:

Example 2:

// C# program to illustrate the interface
using System;
 
// interface declaration
interface Vehicle {
     
    // all are the abstract methods.
    void changeGear(int a);
    void speedUp(int a);
    void applyBrakes(int a);
}
 

class Bicycle : Vehicle{
     
    int speed;
    int gear;
    public void changeGear(int newGear)
    {
        gear = newGear;
    }
    
    public void speedUp(int increment)
    {
         speed = speed + increment;
    }
    
    public void applyBrakes(int decrement)
    {
      speed = speed - decrement;
    }
     
    public void printStates() 
    {
        Console.WriteLine("speed: " + speed + " gear: " + gear);
    }
}
 

class Bike : Vehicle {
     
    int speed;
    int gear;
     
   
    public void changeGear(int newGear)
    {
       gear = newGear;
    }
     
  
    public void speedUp(int increment)
    {
       speed = speed + increment;
    }
 
    public void applyBrakes(int decrement)
    {
         speed = speed - decrement;
    }
     
    public void printStates() 
    {
        Console.WriteLine("speed: " + speed +  " gear: " + gear);
    }
     
}
 
class CodeCofig {
     
    // Main Method
    public static void Main(String []args) 
    {
        Bicycle bicycle = new Bicycle();
        bicycle.changeGear(2);
        bicycle.speedUp(3);
        bicycle.applyBrakes(1);
        Console.WriteLine("Bicycle present state :");
        bicycle.printStates();
         
  
        Bike bike = new Bike();
        bike.changeGear(1);
        bike.speedUp(4);
        bike.applyBrakes(3);
        Console.WriteLine("Bike present state :");
        bike.printStates();
    }
}

Output:

Bicycle    present    state    :
speed:    2    gear:    2
Bike    present    state    :
speed:    1    gear:    1

Advantage of Interface:

  1. It is have used to achieve loose coupling.
  2. It is have used to achieve total abstraction.
  3. To achieve component-based programming
  4. To achieve multiple inheritance and abstraction.
  5. Interfaces add a plug and play similar as architecture into applications.

Leave a Reply

Your email address will not be published. Required fields are marked *