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 ");
- }
- }
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 // i can't fly .... i will Squeak
{ - 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 ");
- }
}
- Humam o = new Fishermam ();
- o.display();
- o.walk();
- o.dancing();
- Humam o = new Handicapmam();
- o.display();
- o.performdance();
- o.swim();
- o.performfight();
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");
- }
}
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");
- }
- }
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();
25. But we still have enhanced these humans by setting the fight and dance behavior dynamically.
The full code is here:
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void but_test_Click(object sender, EventArgs e)
- {
- Humam o = new Fishermam();
- o.display();
- o.performdance();
- o.swim();
- o.performfight();
- }
- private void but_mal_duck_Click(object sender, EventArgs e)
- {
- Humam o = new paintermam();
- o.display();
- o.performdance();
- o.swim();
- o.performfight();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Humam o = new Handicapmam();
- o.display();
- o.performdance();
- o.swim();
- o.performfight();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Humam o = new autismmam ();
- o.display();
- o.performdance();
- o.swim();
- o.performfight();
- }
- }
- 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 interface Idanceable
- {
- void Dance();
- }
- public class simpledance:Idanceable
- {
- public virtual void Dance()
- {
- MessageBox.Show("I Can perform simple dance ");
- }
- }
- public class breakdance : Idanceable
- {
- public virtual void Dance()
- {
- MessageBox.Show("i can do break dance ");
- }
- }
- public interface Ifightable
- {
- void Fight();
- }
- 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");
- }
- }
- public class Fishermam : Humam
- {
- public Fishermam()
- {
- dnce = new simpledance();
- fight = new Fightwithhand();
- }
- public override void display()
- {
- MessageBox.Show(" i am a Fisher mam");
- }
- }
- public class paintermam : Humam
- {
- public paintermam()
- {
- dnce = new simpledance();
- fight = new Fightwithhand();
- }
- public override void display()
- {
- MessageBox.Show(" I am a painter mam");
- }
- }
- public class Handicapmam : Humam // i can't fly .... i will Sqeak
- {
- public Handicapmam()
- {
- dnce = new simpledance();
- fight = new Fightnoway();
- }
- public override void swim()
- {
- MessageBox.Show(" i can swim only for small distance");
- }
- public override void display()
- {
- MessageBox.Show(" i am a Handicap");
- }
- }
- public class autismmam : Humam // i can't fly .... i will Sqeak
- {
- public autismmam()
- {
- dnce = new simpledance();
- fight = new Fightnoway();
- }
- public override void display()
- {
- MessageBox.Show(" i am an autism");
- }
- }

Rajesh BishtPosted Dec 27, 2014, 1:38 PM
really impress !!!
Jitendra KumarPosted Dec 27, 2014, 4:18 AM
Good..