Introduction
In this blog, we will learn about the Interfaces in C#, so let's begin with an introduction of the Interface and then we will learn how we create them with their examples. Interfaces, like a class, can include members such as methods, properties, events, and indexers. However, interfaces will just include the member declarations. The implementation of the interface's members will be provided by a class that either implicitly or explicitly implements the interface.
What are Interfaces?
In C#, an interface is a class's blueprint. It's similar to an abstract class in that all of the methods specified within the interface are abstract methods. It is not capable of having a method body or being instantiated.
It is used to accomplish multiple inheritances that aren't possible with classes. Because it can't contain a method body, it's utilized to achieve complete abstraction.
It must be implemented using a class or struct. All of the methods defined inside the interface must be implemented by the class or struct that implements the interface.
Some considerations to keep in mind while utilizing a C# interface:
- Interfaces, like abstract classes, cannot be used to construct objects.
- Interfaces define what a class must do, not how it must do it.
- Private members are not allowed in interfaces.
- You must override all of an interface's methods when implementing it.
- Properties and methods are allowed in interfaces, but fields and variables are not.
- By default, interface members are abstract and public.
- The interface will always be defined with the help of the keyword 'interface.'
- An interface cannot contain a constructor (as it cannot be used to create objects)
- Multiple inheritances are allowed via Interfaces, but not with classes.
Uses
Following are the advantages of using an Interface in C#:
- It's a technique used for achieving loose coupling.
- It's a technique used for achieving complete abstraction.
- Used to make component-based programming a reality
- Multiple inheritance and abstraction can be achieved.
- Interfaces also provide apps with a plug-and-play design.
How are they created?
To use an interface, we have to use the keyword "interface." Its purpose is to give complete abstraction. This means that all interface members are defined with an empty body and are public and abstract by default. All of the methods defined in the interface must be implemented by a class that implements the interface.
Syntax for Declaration:
interface <interface_name > { // declare Events // declare indexers // declare methods // declare properties } |
Syntax for Implementing:
class class_name : interface_name |
Examples
Let's see the example of the interface in C#, which has the drives() method. Its implementation is provided by two classes: Car and Bike.
using System;
public interface Drivable
{
void drives();
}
public class Car : Drivable
{
public void drives()
{
Console.WriteLine("driving car...");
}
}
public class Bike : Drivable
{
public void drives()
{
Console.WriteLine("driving bike...");
}
}
public class TestInterface
{
public static void Main()
{
Drivable d;
d = new Car();
d.drives();
d = new Bike();
d.drives();
}
}
Output:
driving car... driving bike... |