Friday, June 29, 2012

Polymorphism Interfaces

Source Code Polymorphism Interfaces Project

Polymorphism is used for converting from the derived class to the base class. The derived class is the class that's inheriting/implementing and the base class is the class that's being inherited/implemented. You can then of course only use the public methods, properties and global variables of the base class.

This doesn't seem very exciting, but you can also convert the derived class to the base interface. When done that, the interface object can all of a sudden be used to call methods and properties. You will be only able to use the methods and properties the interface has, but the actual code being executed will be of the derived class. 

Create a new console application called "Polymorphism Tutorial".


I want to show you an example with the interface "ICivilian" and the classes "Man" and "Woman". Add an interface called "ICivilian", then add the following code to the interface:
string Name
{
get;
set;
}
void PayTaxes();
Each civilian has a name and has a method to pay their taxes.


Add a new class called "Woman" and make it implement "ICivilian", then add the following code to "Woman":
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void PayTaxes()
{
Console.WriteLine("Paying taxes");
}
public void NurseWound()
{
Console.WriteLine("Nursing wounds");
}
The interface's methods are implemented in the "Woman" class with an extra method called "NurseWounds".


Now add a class called "Man" and make it implement "ICivilian" as well. Add the following code to the class "Man":
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void PayTaxes()
{
Console.WriteLine("Paying taxes");
}
public void FixCar()
{
Console.WriteLine("Fixing car");
}
The method and property of the interface "ICivilian" have been implemented with an extra method called "FixCar".


Now to initialize the objects for "Man" and "Woman", and to give them both a name in the "Program" class' "Main" method:
Man man = new Man();
Woman woman = new Woman();
man.Name = "Bob";
woman.Name = "Sarah";


All that's left is polymorphism, so add the following code to your "Main":
ICivilian civilianOne = man;
ICivilian civilianTwo = woman;
Above code converts the "Man" object and "Woman" object to "ICivilian" objects. This can be done, because "Man" and "Woman" implemented "ICivilian".


Now to test the code, so you can see that the content of the properties and methods being called are actually of the derived class and not of the base interface. Not that it would be possible that the content would be of the interface, since interfaces can't have content. Add the following code to your "Main" method:
Console.WriteLine(civilianOne.Name);
Console.WriteLine(civilianTwo.Name);
civilianOne.PayTaxes();
civilianTwo.PayTaxes();
Console.Read();
You can try to call the methods "FixCar" and "NurseWound", but "ICivilian" doesn't know these methods.


Run your application by pressing F5. The names of the "ICivilian" objects should be displayed with twice the text "Paying taxes".

I found it hard to make this tutorial, so it took longer than it normally does. If this tutorial is difficult to understand, then I want input at what part you are having trouble with. You can of course do this with every tutorial, but this is the first time I felt uncertain about a tutorial.

Source Code Polymorphism Interfaces Project

No comments:

Post a Comment