What is interface?
The definitions for a group of related functionalities that a non-abstract class or struct can declare is called an interface. The interface is another way of abstraction in C#. The interface is like the abstract class but it only contains the methods definitions, whereas the abstract class contains all methods, fields, constants definitions. Unlike the abstract class, the interface cannot have a constructor. The interface methods should be abstract and public. The interface only provides the definition for the methods. In C# multiple inheritances cannot be done in class but by means of the interface.
The interface in C#:
- Members in the interface should be abstract and public only no other access specifier used.
- By interface, we can’t able to create an object like an abstract class.
- Interface only define the methods, the class needed to override and implement those methods.
- The interface doesn’t contain a constructor.
- The interface contains methods and properties but not fields.
Implementation of C# interface:
The interface keyword used to implement the interface. We can also implement multiple interfaces for a single class in C#. It's suggested to use I before every interface name to identify it has the interface. To use an interface in a class we use (:) to access the interface. For example, demo as interface name then use IDemo
.
Syntax:
interface <interface_name> { method definition; } class <class_name>:<interface_name> { interface_method declaration; }
Example:
using System; interface IDemo1 { void simpleInterface(); } public class Demo:IDemo1{ public void simpleInterface(){ Console.WriteLine("Implementation of Idemo1 interface"); } } public class Program { public static void Main() { Demo d=new Demo(); d.simpleInterface(); Console.ReadLine(); } }
Here I implement an interface called IDemo1
. Then I use the IDemo1
interface in Demo
class using the operator (:)
. Then I implement the interface method simpleInterface
by declaring an output statement which displays Implementation IDemo1 interface.
Multiple interfaces:
To use multiple interfaces, simply use a comma between the interfaces name in class. The above program can be implemented as multiple interfaces with another interface called Idemo2
.
using System; interface IDemo1 { void simpleInterface1(); } interface IDemo2 { void simpleInterface2(); } public class Demo:IDemo1,IDemo2{ public void simpleInterface1(){ Console.WriteLine("Implementation of Idemo1 interface"); } public void simpleInterface2(){ Console.WriteLine("Implementation of Idemo2 interface"); } } public class Program { public static void Main() { Demo d=new Demo(); d.simpleInterface1(); d.simpleInterface2(); Console.ReadLine(); } }
From C# 8.0
onwards we can implement a default implementation for the interface methods itself. We can also use other access specifiers such as private, protected, internal, public, virtual, abstract, override, sealed, static, extern
. We can also implement static methods in interfaces.
For reference https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions
For my other programming post -> Go.
Top comments (0)