We have two interfaces IMan and IBirds. Both the interfaces defines a method named Eat with same name and signature. A class MyClass is inheriting both the interfaces. How can we implement method of both the interfaces in the derived class MyClass ?
Here is the code:
| 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 | publicinterfaceIMan{voidEat();}publicinterfaceIBirds{voidEat();}publicclassMyClass : IMan, IBirds{voidIMan.Eat(){Console.WriteLine("Roti");}voidIBirds.Eat(){Console.WriteLine("Earthworms");}} | 
We use interface name to access its method to implement.
Similarly, we call a method by instantiating the desired interface and assigning object of the derived class to it.
| 
1 
2 
3 
4 
5 
6 
7 
8 
9 | IMan man = newMyClass();man.Eat();IBirds birds = newMyClass();birds.Eat();//OR((IMan)newMyClass()).Eat();((IBirds)newMyClass()).Eat(); | 
 
No comments:
Post a Comment