fork download
  1. using System;
  2. /*
  3.  ////interface////
  4.  * Interfaces are used to achieve multiple inheritance in C#.
  5.  * Similar to abstract classes, interfaces help us to achieve abstraction in C#.
  6.  * Interfaces provide loose coupling - having no or least effect on other parts of code
  7.   when we change one part of a code.
  8.  */
  9. /*
  10.  * Abstract Class *
  11. *It contains both a declaration and implementation parts.
  12. *Multiple inheritance is not achieved by abstract class.
  13. *It contains a constructor.
  14. *It can contain static members.
  15. *It can contain different types of access modifiers like public, private, protected etc.
  16. *The performance of an abstract class is fast.
  17. *It can be fully, partially, or not implemented.
  18.  * Interface *
  19. *It contains only the declaration of methods, properties, and events. Since C# 8,
  20.  default implementations can also be included.
  21. *Multiple inheritance is achieved by interface.
  22. *It does not contain a constructor.
  23. *It does not contain static members.
  24. *It only contains public access modifier because everything in the interface is public.
  25. *The performance of an interface is slow.
  26. *It should be fully implemented.
  27.  */
  28. public interface Drawable
  29. {
  30. void draw();
  31. void print();
  32. }
  33. public class Rectangle : Drawable
  34. {
  35. public void draw()
  36. {
  37. Console.WriteLine("drawing rectangle...");
  38. }
  39. public void print()
  40. {
  41. Console.WriteLine("Rectangle class");
  42. }
  43. }
  44. public class Circle : Rectangle ,Drawable
  45. {
  46. public new void draw()
  47. {
  48. Console.WriteLine("drawing circle...");
  49. }
  50. }
  51. public class TestInterface
  52. {
  53. public static void Main()
  54. {
  55. Drawable d;
  56. d = new Rectangle();
  57. d.draw();
  58. d = new Circle();
  59. d.draw(); d.print();
  60. }
  61. }
Success #stdin #stdout 0.05s 28668KB
stdin
Standard input is empty
stdout
drawing rectangle...
drawing circle...
Rectangle class