img

Interfaces

Interfaces are language structures that determine what methods should be described by the class that implements this interface. If a programmer creates a class that implements a particular interface, this class implements all the methods defined in the interface. In order to specify the interface, use the keyword interface

Example. Let's create a class (MyClass) that implements BasicInterface interface:

 interface BasicInterface { void Do1(); void Do2(); } class MyClass : BasicInterface { void Do1() { // Perform any action #1 } void Do2() { // Perform any action #2 } } 

A class can implement multiple interfaces, for which it is necessary to separate a list of interfaces by commas.

Let us see an example of polymorphism, work with classes, interfaces and inheritance.

 // Let us create the interface I and the classes A and B, implementing this interface interface I { void Print(); } class A : I { void Print() { System.Print("Class A"); } } class B : I { void Print() { System.Print("Class B"); } } int Run() { I @i1 = A(); I @i2 = B(); i1.Print(); i2.Print(); return(0); } 

Result:

 Class A Class B