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:
- public abstract class Humam
- {
-
- public virtual void swim()
- {
- MessageBox.Show(" i can swim");
- }
- public abstract void display();
-
- public virtual void dance()
- {
- MessageBox.Show(" i can dance ");
- }
-
- }
- public class Fishermam : Humam
- {
- public Fishermam ()
- {
-
- }
- public override void display()
- {
- MessageBox.Show(" i am a Fisher”);
- }
- }
-
- public class paintermam : Duck
- {
- public paintermam ()
- {
-
- }
- public override void display()
- {
- MessageBox.Show(" i am a Painter ");
- }
- }
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”.
- public class Handicapmam : Duck
{
-
- public Handicapmam ()
- {
-
- }
- public override void swim()
- {
- MessageBox.Show(" i can swim only for small distance");
- }
- public override void display()
- {
- MessageBox.Show(" i am a Handicap … I am depressed ");
- }
}
9. Now let's initiate this Fisherman and Handicappedman.
- Humam o = new Fishermam ();
- o.display();
- o.walk();
- o.dancing();
-
- Humam o = new Handicapmam();
- o.display();
- o.performdance();
- o.swim();
- 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.
- public abstract class Humam
- {
- public Idanceable dnce;
- public Ifightable fight;
- public virtual void swim()
- {
- MessageBox.Show(" i can swim");
- }
- public abstract void display();
- public void performdance()
- {
- dnce.Dance();
- }
- public void performfight()
- {
- fight.Fight();
- }
-
-
- public class Fishermam : Humam
-
- public Fishermam()
- {
- dnce = new simpledance();
- fight = new Fightwithhand();
- }
- public override void display()
- {
- MessageBox.Show(" i am a Fisher mam");
- }
}
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.
- public class Fightwithhand : Ifightable
- {
- public virtual void Fight()
- {
- MessageBox.Show(" i can Fight with hand");
- }
- }
- public class Fightwithsword : Ifightable
- {
- public virtual void Fight()
- {
- MessageBox.Show(" i can Fight with sword");
- }
- }
- public class Fightnoway : Ifightable
- {
- public virtual void Fight()
- {
- MessageBox.Show(" Sorry i can't Fight");
- }
- }
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.
- Humam o = new paintermam(); or Humam o = new Handicapmam();
- So on…
- o.display();
- o.performdance();
- o.swim();
- 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: