Understanding Interfaces


Published on

Understanding Interfaces, Explicit Interface, Interface Implimentation

Published in: Technology, Education
1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Understanding Interfaces

  1. 1. Understanding Interfaces Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com
  2. 2. Interfaces are simplest construct of C#. But understanding when and why to use them can take years… www.dotnetvideotutorial.com
  3. 3. In this session let us focus on understanding interface as a C# construct. We will discuss one of its usability in next session “Dependency Injection” where we will see how we can create loosely coupled systems using interfaces
  4. 4. MSDN Defines Interfaces as…
  5. 5. An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
  6. 6. Let us try to understand using simple example. Let us create simple interface IDemoInterface with two methods defined in it… www.dotnetvideotutorial.com
  7. 7. By default every member of interface is public so interface doesn„t allow use of access modifier.
  8. 8. Interface doesn't have any implementation. We have to implement members defined by interface in class that we will drive from interface.
  9. 9. That‟s why we never say that “we are deriving class from interface” Instead we say that “we are implementing interface in class”
  10. 10. Let us implement IDemoInterface in DemoClass www.dotnetvideotutorial.com
  11. 11. Interface implementation syntax is same as class inheritance. We need to implement all methods of IDemoInterface in DemoClass
  12. 12. Now we have two ways to create object of DemoClass.
  13. 13. Normal Instantiation: Using reference of DemoClass only
  14. 14. Or www.dotnetvideotutorial.com
  15. 15. Upcasting: Using reference of IDemoInterface
  16. 16. And in both the cases output will be same...
  17. 17. Now let us add little complexity to our example. Assume that there is one more interface ISampleInterface with two methods… www.dotnetvideotutorial.com
  18. 18. This interface have two methods fun2() and fun3(). We have intentionally kept fun2() in both the interfaces .
  19. 19. C# allows multiple interface implementation. Let us implement both the interfaces in DemoClass Imp Note: Multiple class inheritance is not allowed in C#. i.e. we can‟t derive class from two or more classes
  20. 20. Now we have three ways to create object of DemoClass www.dotnetvideotutorial.com
  21. 21. or or
  22. 22. First let us have normal instantiation…
  23. 23. Output: www.dotnetvideotutorial.com
  24. 24. But in this case we have assumed that fun2() of both the interfaces has common implementation. Go back and check in DemoClass we have single implementation of fun2()
  25. 25. What if we wanted to provide separate implementations for fun2() of IDemoInterface and ISampleInterface? www.dotnetvideotutorial.com
  26. 26. We can do it using Explicit Interface Implementation
  27. 27. Now fun2() is explicitly implemented and have two separate implementations. Also observe that explicitly implemented methods can‟t have access modifier in DemoClass as they will be always public
  28. 28. We can’t call explicitly implemented methods using reference of class. www.dotnetvideotutorial.com
  29. 29. We can call only fun1() and fun3() using reference of DemoClass. Compiler will not able to resolve method call to fun2() as there are two implementation on fun2() in DemoClass.
  30. 30. Million dollar question How to call fun2()?
  31. 31. We can call explicitly implemented methods using reference of Interface.
  32. 32. So to call fun2() of IDemoInterface we can create object of DemoClass using reference of IDemoInterface www.dotnetvideotutorial.com
  33. 33. Interface reference has access to all its methods.
  34. 34. Output:
  35. 35. Same way to call fun2() of ISampleInterface we can create object of DemoClass using reference of ISampleIneterface
  36. 36. www.dotnetvideotutorial.com
  37. 37. Output:
  38. 38. Note that Interface reference has access to its own methods only. Reference of one interface can‟t call method of other interface.
  39. 39. Is there any way out to call explicitly implemented method using reference of class?
  40. 40. Yes! We can typecast reference of class to interface www.dotnetvideotutorial.com
  41. 41. Here we are typecasting reference of DemoClass ; first to IDemoInterface and then to ISampleInterface.
  42. 42. Output:
  43. 43. We can also derive one interface from other.. www.dotnetvideotutorial.com
  44. 44. Here IDerivedInterface is derived from IBaseInterface
  45. 45. So we have to implement methods of both the interfaces in class which implements IDerivedInterface
  46. 46. In next session we will have look on how to use interfaces to create loosely coupled systems. www.dotnetvideotutorial.com
  47. 47. For video visit www.dotnetvideotutorial.com
  48. 48. Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com