Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used to achieve full abstraction because it can’t have normal methods. In C#, multiple inheritance is achieved by interface.
Interface can’t be instantiated. If a class is implementing an interface then it must give definition to all of its methods.
Let’s see an example of interface in C#.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; public interface Drawable { void show(); } public class Circle:Drawable { public void show() { Console.WriteLine("drawing Circle...."); } } public class InterfaceExample { public static void Main (string[] args) { Drawable d; d=new Circle(); d.show(); } } |
Output
1 2 3 |
drawing Circle.... |