Open In App

C# Interface

Last Updated : 05 Apr, 2025
Suggest changes
Share
Like Article
Like
Report

An interface is defined using the interface keyword, similar to a class. An Interface is a blueprint that outlines a group of methods, properties, events, or indexers that a class or struct must implement. Unlike classes, it contains only the declaration of the members. The implementation of the interface’s members will be given by a class that implements the interface implicitly or explicitly. There are some important points to remember, as mentioned below:

  • Interfaces cannot have private members.
  • By default, all the members of Interface are public and abstract.
  • The interface will always be defined with the help of the keyword interface
  • Interfaces cannot contain fields because they represent an implementation of data.
  • We can achieve multiple inheritance with the interface but not with the classes.
  • We can implement multiple interfaces in a single class separated by commas.

Example 1: Demonstrating the basic case for implementing the Interface.

C#
// Demonstrate working of Interface  using System;   interface inter1 { // method having only declaration  // with no definition void display(); } // Implementing inteface in Geeks class Geeks : inter1 { // providing the body part of function  public void display() { Console.WriteLine("Demonstration of interface"); }  // Main Method public static void Main(String[] args) { Geeks t = new Geeks(); t.display(); } } 

Output
Demonstration of interface 


Example 2: Implementation of interfaces and the use of polymorphism in C#

C#
// Using Interfaces and Polymorphism using System; // interface declaration  interface Vehicle { // abstract method.  void speedUp(int a); } // class implements interface  class Bicycle : Vehicle { int speed; // Method increase speed  public void speedUp(int increment) { speed = speed + increment; } // Method check speed public void CheckSpeed() { Console.WriteLine("speed: " + speed); } } // class implements interface  class Bike : Vehicle { int speed;   // to increase speed  public void speedUp(int increment) { speed = speed + increment; } public void CheckSpeed() { Console.WriteLine("speed: " + speed); } } class Geeks { public static void Main(String[] args) { // creating an instance of Bicycle  // doing some operations  Bicycle bicycle = new Bicycle(); bicycle.speedUp(3); Console.WriteLine("Bicycle present state :"); bicycle.CheckSpeed(); // creating instance of bike.  Bike bike = new Bike(); bike.speedUp(4); Console.WriteLine("Bike present state :"); bike.CheckSpeed(); } } 

Output
Bicycle present state : speed: 3 Bike present state : speed: 4 

Advantages of Interfaces

  • Loose coupling: It is used to achieve loose coupling rather than concrete implementations, we can decouple classes and reduce interdependencies.
  • Abstraction: Interface helps to achieve full abstraction.
  • Maintainability: It helps to achieve component-based programming that can make code more maintainable.
  • Multiple Inheritance: Interface used to achieve multiple inheritance and abstraction.
  • Define architecture: Interfaces add a plug and play like architecture into applications.
  • Modularity: Promote a cleaner and more modular design. By separating the definition of behavior from the implementation

Similar Reads

Article Tags :
Practice Tags :