A simple example on achieving polymorphism in ASP.Net application part 1
In one of my ASP.Net seminars I have been asked to give an example of the possible ways we can achieve Polymorphism in our ASP.Net applications.
So I have decided to write a simple ASP.Net application that demonstrates the use of Polymorphism. I assume that the people who will read on this post understand basic OOP concepts, like Inheritance and Encapsulation.
Well, the first thing one must do is to read this post of mine where I talk about array of objects, arraylists and generic collections. You must understand the concepts explained in this post very well. I will use the same custom types/classes in this example as well. I will try to solve the problem we originally had with Generic collections. In the last paragraph of this post ,I tried to add an object of type Animal to the generic list people. This failed of course at compile time as we cannot add an animal object in the people collection. Let's imagine that we need to add both objects in a collection and then a call the method on each object in that collection even though each object is of different type. If that does not make sense, please read on. In order to achieve that we need to implement some sort of polymorphism.We will see how to achieve polymorphism through Inheritance from a base class. 1) Launch Visual Studio 2010/2008/2005. Express editions work fine. 2) Create a new empty website and give it an appropriate name.Choose C# as the development language. 3) Add a new item in your site, a web form item. Leave the default name, so it is called Default.aspx. 4) Add a new item in your site, a class file.Name the class file Person.cs. 5) The whole code for the Person.cs class follows 6) Add another class file to your site and name is Animal.cs. The code inside the .cs file should be something like this 10) I create a generic collection of type Being and I call it Living. Then I create 3 instances of the Person custom object and fill in the properties with values. Then I use the Add method to add those objects in my Living collection. Then I just loop through the items in the list and displaying information about them by calling the DisplayInfo() method.public class Person
{
#region define public properties of my person class
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public decimal Height { get; set; }
public decimal Weight { get; set; }
#endregion
public string DisplayInfo()
{
string info;
info = "FullName is: " + FirstName + " " + LastName;
return info;
}
}public class Animal
{
public bool isMammal { get; set; }
public string Species { get; set; }
public double Speed { get; set; }
public decimal Height { get; set; }
public decimal Weight { get; set; }
} public string DisplayInfo()
{
string info;
info = "Species is: " + Species + " and its speed is: " + Speed;
return info;
}
7) Add another class file to your site and name is Being.cs. This will be the base class that the other two classes will inherit from.
The code inside the .cs file should be something like this
public class Being
{
public int BeingID { get; set; }
public string DisplayInfo()
{
string info;
info = "Being is: " + BeingID;
return info;
}
}
8) Now we must make some changes to the base and child classes. We should mark the DisplayInfo() method as virtual in the Being class. The complete class is this
public class Being
{
public int BeingID { get; set; }
public virtual string DisplayInfo()
{
string info;
info = "Being is: " + BeingID;
return info;
}
} The Person class must inherit from the Being class. The DisplayInfo() method of the Person class must access the functionality of the base class DisplayInfo() method and also has its own implementation.We mark the DisplayInfo() method of the Person class with the override keyword.To access the functionality of the DisplayInfo() method of the base class we use this bit of code
base.DisplayInfo();
Please see below for complete implementation of the Person class
public class Person : Being
{
#region define public properties of my person class
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public decimal Height { get; set; }
public decimal Weight { get; set; }
#endregion
public override string DisplayInfo()
{
string myinfo= base.DisplayInfo();
string info;
info = myinfo + " and " + "FullName is: " + FirstName + " " + LastName;
return info;
}
} The Animal class must inherit from the Being class. The DisplayInfo() method of the Animal class must access the functionality of the base class DisplayInfo() method and also has its own implementation.
We mark the DisplayInfo() method of the Animal class with the override keyword.To access the functionality of the DisplayInfo() method of the base class we use this bit of code
base.DisplayInfo();
Please see below for complete implementation of the Animal class.
public class Animal:Being
{
public bool isMammal { get; set; }
public string Species { get; set; }
public double Speed { get; set; }
public decimal Height { get; set; }
public decimal Weight { get; set; }
public override string DisplayInfo()
{
string myinfo = base.DisplayInfo();
string info;
info =myinfo + " and " + "Species is: "+Species+" and its speed is: "+Speed;
return info;
}}
9) In the Page_Load() event handling routine of the Default.aspx page type
List<Being> Living = new List<Being>();
Person p1 = new Person();
p1.BeingID = 0;
p1.FirstName = "Nikos";
p1.LastName = "Kantzelis";
p1.Email = "nikolaosk@hotmail.com";
p1.Height = 1.78m;
p1.Weight = 88.5m;
Person p2 = new Person();
p2.BeingID = 1;
p2.FirstName = "James";
p2.LastName = "Rowling";
p2.Email = "jamesr@hotmail.com";
p2.Height = 1.98m;
p2.Weight = 98.25m;
Person p3 = new Person();
p3.BeingID = 3;
p3.FirstName = "George";
p3.LastName = "Graham";
p3.Email = "graham@yahoo.co.uk";
p3.Height = 1.88m;
p3.Weight = 81.5m;
Animal myanimal = new Animal();
myanimal.BeingID = 4;
myanimal.isMammal = true;
myanimal.Species = "Panthera";
myanimal.Speed = 56.8;
myanimal.Height = 0.78m;
myanimal.Weight = 163;
Living.Add(p1);
Living.Add(p2);
Living.Add(p3);
Living.Add(myanimal);
foreach (Being item in Living)
{
Response.Write(item.DisplayInfo());
Response.Write("<br/>");
}List<Being> Living = new List<Being>();
11) Build and run your application.If you followed everything correctly until now your application will compile.I urge you to add many breakpoints in the application and see the flow of the execution. We were able to get two different types to be added to the same collection of objects and make them call the same method DisplayInfo(). Then I create 1 instance of the Animal custom object and fill in the properties with values.
I know this is a very simple example but nevertheless it demonstrates how to use Polymorphism with a base class and Inheritance.
Hope it helps.Email me if you need the source code.