Look at the following points:

1. Consider the following example.

2. Now our code will be like the following:

  1. public abstract class Humam
  2. {
  3. public virtual void swim()
  4. {
  5. MessageBox.Show(" i can swim");
  6. }
  7. public abstract void display();
  8. public virtual void dance()
  9. {
  10. MessageBox.Show(" i can dance ");
  11. }
  12. }
  13. public class Fishermam : Humam
  14. {
  15. public Fishermam ()
  16. {
  17. }
  18. public override void display()
  19. {
  20. MessageBox.Show(" i am a Fisher”);
  21. }
  22. }
  23. public class paintermam : Duck
  24. {
  25. public paintermam ()
  26. {
  27. }
  28. public override void display()
  29. {
  30. MessageBox.Show(" i am a Painter ");
  31. }
  32. }
3. So now it's perfect. We have code reusability through inheritance.

4. Suddenly there is a behavior to be added, say fighting.

5. Here is the solution. Let's add it to the abstract class so that the fisherman and painter also fight.

6. Here comes another new type of people named “Handicapped Man”.

7. But these handicapped person will not swim, so now what to do?

8. Ya ya; let's override the super class and change the behavior to “NO swiming”.
  1. public class Handicapmam : Duck // i can't fly .... i will Squeak
    {
  2. public Handicapmam ()
  3. {
  4. }
  5. public override void swim()
  6. {
  7. MessageBox.Show(" i can swim only for small distance");
  8. }
  9. public override void display()
  10. {
  11. MessageBox.Show(" i am a Handicap … I am depressed ");
  12. }
    }
9. Now let's initiate this Fisherman and Handicappedman.
  1. Humam o = new Fishermam ();
  2. o.display();
  3. o.walk();
  4. o.dancing();
  5. Humam o = new Handicapmam();
  6. o.display();
  7. o.performdance();
  8. o.swim();
  9. o.performfight();
10. Hey, wait; Handicappedman can't fight, right?

11. What to do? Let's override to nothing. Is this is the best idea?

12. So if we use an interface then both the painter and fisherman will fight using an interface.

13. So both will use the same interface to achieve fighting behavior, thus whoever can fight will use the Ifightable interface.

14. How adout an interface?

15. Let's do some real work. While thinking broader we realize that some people can dance whereas some can't, some people fight whereas some can't. Some people can't do anything.

16. Let's figure out what property will continue to change and provide them an interface.

17. Now we are up to the task. All people will have display behavior. So it will be an abstract class. Fisherman and painter will share the common behavior of dancing and fighting. Handicapman can't fight and they do a simple dance.

18. Now how do those two fight and is the dance behavior set? Using the constructor of the corresponding class, right.
  1. public abstract class Humam
  2. {
  3. public Idanceable dnce;
  4. public Ifightable fight;
  5. public virtual void swim()
  6. {
  7. MessageBox.Show(" i can swim");
  8. }
  9. public abstract void display();
  10. public void performdance()
  11. {
  12. dnce.Dance();
  13. }
  14. public void performfight()
  15. {
  16. fight.Fight();
  17. }
  18. public class Fishermam : Humam
  19. public Fishermam()
  20. {
  21. dnce = new simpledance();
  22. fight = new Fightwithhand();
  23. }
  24. public override void display()
  25. {
  26. MessageBox.Show(" i am a Fisher mam");
  27. }
    }
19. That's it.

20. Hey suppose the fisherman is good at fighting with a stick.

21. Now what? We can create a new class that implements the interface so that if any other type of person can fight with a stick, he can use this class.
  1. public class Fightwithhand : Ifightable
  2. {
  3. public virtual void Fight()
  4. {
  5. MessageBox.Show(" i can Fight with hand");
  6. }
  7. }
  8. public class Fightwithsword : Ifightable
  9. {
  10. public virtual void Fight()
  11. {
  12. MessageBox.Show(" i can Fight with sword");
  13. }
  14. }
  15. public class Fightnoway : Ifightable
  16. {
  17. public virtual void Fight()
  18. {
  19. MessageBox.Show(" Sorry i can't Fight");
  20. }
  21. }
22. These behaviors can be set depending on the ability to fight.

23. Now we have achieved OOP concepts. Let's now create those humans.

  1. Humam o = new paintermam(); or Humam o = new Handicapmam();
  2. So on…
  3. o.display();
  4. o.performdance();
  5. o.swim();
  6. o.performfight();
24. We came to understand the exact use of interfaces and we also achieved code reusability.

25. But we still have enhanced these humans by setting the fight and dance behavior dynamically.

The full code is here:
  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6. }
  7. private void but_test_Click(object sender, EventArgs e)
  8. {
  9. Humam o = new Fishermam();
  10. o.display();
  11. o.performdance();
  12. o.swim();
  13. o.performfight();
  14. }
  15. private void but_mal_duck_Click(object sender, EventArgs e)
  16. {
  17. Humam o = new paintermam();
  18. o.display();
  19. o.performdance();
  20. o.swim();
  21. o.performfight();
  22. }
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. Humam o = new Handicapmam();
  26. o.display();
  27. o.performdance();
  28. o.swim();
  29. o.performfight();
  30. }
  31. private void button2_Click(object sender, EventArgs e)
  32. {
  33. Humam o = new autismmam ();
  34. o.display();
  35. o.performdance();
  36. o.swim();
  37. o.performfight();
  38. }
  39. }
  40. public abstract class Humam
  41. {
  42. public Idanceable dnce;
  43. public Ifightable fight;
  44. public virtual void swim()
  45. {
  46. MessageBox.Show(" i can swim");
  47. }
  48. public abstract void display();
  49. public void performdance()
  50. {
  51. dnce.Dance();
  52. }
  53. public void performfight()
  54. {
  55. fight.Fight();
  56. }
  57. }
  58. public interface Idanceable
  59. {
  60. void Dance();
  61. }
  62. public class simpledance:Idanceable
  63. {
  64. public virtual void Dance()
  65. {
  66. MessageBox.Show("I Can perform simple dance ");
  67. }
  68. }
  69. public class breakdance : Idanceable
  70. {
  71. public virtual void Dance()
  72. {
  73. MessageBox.Show("i can do break dance ");
  74. }
  75. }
  76. public interface Ifightable
  77. {
  78. void Fight();
  79. }
  80. public class Fightwithhand : Ifightable
  81. {
  82. public virtual void Fight()
  83. {
  84. MessageBox.Show(" i can Fight with hand");
  85. }
  86. }
  87. public class Fightwithsword : Ifightable
  88. {
  89. public virtual void Fight()
  90. {
  91. MessageBox.Show(" i can Fight with sword");
  92. }
  93. }
  94. public class Fightnoway : Ifightable
  95. {
  96. public virtual void Fight()
  97. {
  98. MessageBox.Show(" Sorry i can't Fight");
  99. }
  100. }
  101. public class Fishermam : Humam
  102. {
  103. public Fishermam()
  104. {
  105. dnce = new simpledance();
  106. fight = new Fightwithhand();
  107. }
  108. public override void display()
  109. {
  110. MessageBox.Show(" i am a Fisher mam");
  111. }
  112. }
  113. public class paintermam : Humam
  114. {
  115. public paintermam()
  116. {
  117. dnce = new simpledance();
  118. fight = new Fightwithhand();
  119. }
  120. public override void display()
  121. {
  122. MessageBox.Show(" I am a painter mam");
  123. }
  124. }
  125. public class Handicapmam : Humam // i can't fly .... i will Sqeak
  126. {
  127. public Handicapmam()
  128. {
  129. dnce = new simpledance();
  130. fight = new Fightnoway();
  131. }
  132. public override void swim()
  133. {
  134. MessageBox.Show(" i can swim only for small distance");
  135. }
  136. public override void display()
  137. {
  138. MessageBox.Show(" i am a Handicap");
  139. }
  140. }
  141. public class autismmam : Humam // i can't fly .... i will Sqeak
  142. {
  143. public autismmam()
  144. {
  145. dnce = new simpledance();
  146. fight = new Fightnoway();
  147. }
  148. public override void display()
  149. {
  150. MessageBox.Show(" i am an autism");
  151. }
  152. }