Interface in C#
Why Use Interface?
In programming, we use interface when we have some same functionalities in different classes, but all the functionalities are performed differently so to solve this issue we use interface.
The interface is implemented by different classes and every class that implements an interface must have a definition of all the abstract methods of the attributes and functions of that interface.
The interface tells the functionality i.e. Signature of the functions and the class which implements the interface perform the functionality i.e. Provide definition/body of the abstract methods of the interface. How for this interface? If we have different animals classes, and we know every animal eat, but all the animals do not eat the same things so we just declare the eat function in the interface and all the classes which implement that interface must define them.
Syntax:
1 2 3 4 5 |
public interface animals{ void eats(); } |
Above is the simple syntax of an interface. The interface has functions and attributes, but not their body and if we try to define them within the interface, then it generates an error.
Multiple Inheritance in Interface:
Multiple inheritances are also possible in the case of interfaces. Which we can’t do in classes i.e. One class can’t extend more than one classes, but a class can implement more than one interface.
Example
1 2 3 4 5 6 7 8 |
public interface Reptile { } public interface Animal { } public class Crocodile : Reptile, Animal { } |
Now we will see how a class implements an interface.
1 2 3 |
public class dogs : animals { } |
And as in above example the class dogs implements interface animals, so it is compulsory for dog class to define the eat() function in its body.
1 2 3 4 5 6 7 |
public class dog : animals{ void eat(){ System.out.println(“ Dogs eat bones “ ); } } |
The interface is the same as an abstract class, but an abstract class has both abstract and non-abstract function, and the interface has the only abstract function, and more likely we have no need to define them as an abstract function or pure virtual function as in C++.
Understanding Interface
The following example shows the working of the interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System.Collections.Generic; using System.Linq; using System.Text; using System; namespace mydemo { public interface animal //interface { void have_legs(); //abstract method void eat_food(); //abstract method } public class dog : animal //implementing interface { string name; void calling_func(){ have_legs(); eat_food(); } void have_legs(){ Console.WriteLine("Dog has four legs"); } void eat_food(){ Console.WriteLine("Dog like bones to eat"); } } public class chicken : animal //implementing interface { string name; void calling_func(){ have_legs(); eat_food(); } void have_legs(){ Console.WriteLine("chicken has two legs"); } void eat_food(){ Console.WriteLine("chicken likes to eat grains"); } } class main_class { static void Main(string[] args) { dog obj = new dog(); obj.calling_func(); cock obj1 = new cock(); obj1.calling_func(); Console.ReadKey(); } } } |