C# | Interface

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

  • 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 defined 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 Declaration:

interface  <interface_name >
{
    // declare Events
    // declare indexers
    // declare methods 
    // declare properties
}

Syntax for Implementing Interface:

class class_name : interface_name

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the members in the interface are declared with the empty body and are public and abstract by default. A class that implement interface must implement all the methods declared in the interface.

  • Example 1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to demonstrate working of 
    // interface
    using System;
      
    // A simple interface
    interface inter1
    {
        // method having only declaration 
        // not definition
        void display();
    }
      
    // A class that implements interface.
    class testClass : inter1
    {
          
        // providing the body part of function
        public void display()
        {
            Console.WriteLine("Sudo Placement GeeksforGeeks");
        }
      
        // Main Method
        public static void Main (String []args)
        {
              
            // Creating object
            testClass t = new testClass();
              
            // calling method
            t.display();
        }
    }

    chevron_right

    
    

    Output:

    Sudo Placement GeeksforGeeks
    
  • Example 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 implements interface
    class Bicycle : Vehicle{
          
        int speed;
        int gear;
          
        // to change gear
        public void changeGear(int newGear)
        {
              
            gear = newGear;
        }
          
        // to increase speed
        public void speedUp(int increment)
        {
              
            speed = speed + increment;
        }
          
        // to decrease speed
        public void applyBrakes(int decrement)
        {
              
            speed = speed - decrement;
        }
          
        public void printStates() 
        {
            Console.WriteLine("speed: " + speed + 
                              " gear: " + gear);
        }
    }
      
    // class implements interface
    class Bike : Vehicle {
          
        int speed;
        int gear;
          
        // to change gear
        public void changeGear(int newGear)
        {
              
            gear = newGear;
        }
          
        // to increase speed
        public void speedUp(int increment)
        {
              
            speed = speed + increment;
        }
          
        // to decrease speed
        public void applyBrakes(int decrement){
              
            speed = speed - decrement;
        }
          
        public void printStates() 
        {
            Console.WriteLine("speed: " + speed + 
                              " gear: " + gear);
        }
          
    }
      
    class GFG {
          
        // Main Method
        public static void Main(String []args) 
        {
          
            // creating an instance of Bicycle 
            // doing some operations 
            Bicycle bicycle = new Bicycle();
            bicycle.changeGear(2);
            bicycle.speedUp(3);
            bicycle.applyBrakes(1);
              
            Console.WriteLine("Bicycle present state :");
            bicycle.printStates();
              
            // creating instance of bike.
            Bike bike = new Bike();
            bike.changeGear(1);
            bike.speedUp(4);
            bike.applyBrakes(3);
              
            Console.WriteLine("Bike present state :");
            bike.printStates();
        }
    }

    chevron_right

    
    

    Output:

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

Advantage of Interface:

  • It is used to achieve loose coupling.
  • It is used to achieve total abstraction.
  • To achieve component-based programming
  • To achieve multiple inheritance and abstraction.
  • Interfaces add a plug and play like architecture into applications.


  • My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    3


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.