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.

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 that post very well.

Let's start with our example.

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

public class Person
{

    #region  define public properties of my person class

    public string FirstName { getset; }

    public string LastName { getset; }

    public string Email { getset; }

    public decimal Height { getset; }

    public decimal Weight { getset; }


    #endregion

  

  
public string DisplayInfo()
    {

      
        string info;
        info = "FullName is: " + FirstName + " " + LastName;
        return info;

    }
}

6) Add another class file to your site and name is Animal.cs. The code inside the .cs file should be something like this

public class Animal
{

    public bool isMammal { getset; }

    public string Species { getset; }

    public double Speed { getset; }

    public decimal Height { getset; }

    public decimal Weight { getset; }

}
    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 { getset; }

        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 { getset; }

    public string LastName { getset; }

    public string Email { getset; }

    public decimal Height { getset; }

    public decimal Weight { getset; }



    #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 { getset; }

public string Species { getset; }

public double Speed { getset; }

public decimal Height { getset; }

public decimal Weight { getset; }



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/>");

        }

10) I create a generic collection of type Being and I call it Living.

List<Being> Living = new List<Being>();
 

Then I create 3 instances of the Person custom object and fill in the properties with values.

Then I create 1 instance of the Animal 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.

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().
 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.
Email me if you need the source code.
Hope it helps.

6 Comments

  • i have understand well..but i had small doubt in this line" 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(). "..will u clear me .

  • i dont understand the where the difference between abstract and interface implementation.

    you used same representation in both ways(interface or abstract)

  • This will be the proper weblog for anybody who wants to learn about this topic. You comprehend so considerably its pretty much hard to argue with you (not that I really would want?-HaHa). You unquestionably put a new spin on a subject thats been written about for years. Superb stuff, just very good!


    Personalized NFL Jerseys

  • I'm impressed, I should say. Truly rarely do I encounter a blog that's both educative and entertaining, and let me tell you, you might have hit the nail on the head. Your concept is outstanding; the issue is something that not enough individuals are speaking intelligently about. I am especially pleased that I stumbled across this in my search for some thing relating to this.


    michael kors bags

  • I'm impressed, I should say. Certainly rarely do I encounter a blog that's both educative and entertaining, and let me tell you, you have hit the nail on the head. Your idea is outstanding; the concern is something that not enough people are speaking intelligently about. I'm very pleased that I stumbled across this in my search for some thing relating to this.


    Wholesale NFL Jerseys

  • Wow, that's what I was exploring for, what a material! existing here at this web site, thanks admin of this website.|

Comments have been disabled for this content.