Where to Use Interface and How to Achieve Better Programming

Look at the following points:

1. Consider the following example.

  • We will initially start with 3 types of people derived from the Human family.

  • These three types of people share a common behavior, like swimming, dancing and displaying.

2. Now our code will be like the following:

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

 

Up Next
    Ebook Download
    View all
    Learn
    View all