A simple example on achieving polymorphism in ASP.Net application part 2
In this post we will continue exploring the meaning of Polymorphism and the various ways we can implement Polymorphism in ASP.Net applications.
In my previous post I demonstrated how to use Polymorphism through Inheritance and base instance classes. In this example I will use Inheritance through abstract base classes.
This example is going to be very similar like the previous one on Polymorphism. The difference is that we do not want to implement the DisplayInfo() method in the base class.
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 that post
very well. 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.We will make this class as an abstract class.
The code inside the .cs file should be something like this. We provide no implementation for the DisplayInfo() method.
public abstract class Being
{
public int BeingID { get; set; }
public abstract string DisplayInfo();
}
The Person class must inherit from the Being abstract class and must implement the DisplayInfo() method. This method of the Person class must be marked with the override keyword.
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 info;
info = base.BeingID +" and "+"FullName is: "+ FirstName +" " + LastName;
return info;
}
}The Animal class must inherit from the Being abstract class and must implement the DisplayInfo() method. This method of the Animal class must be marked with the override keyword.
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 info;info =base.BeingID + " 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 (and very similar to my other post in Polymorphism) but nevertheless it demonstrates how to use Polymorphism with a base abstract class and Inheritance.
Hope it helps.Email me if you need the source code.