Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

A simple example on achieving polymorphism in ASP.Net application part 3

In this post we will continue exploring the meaning of Polymorphism and the various ways we can implement Polymorphism in ASP.Net applications.

In this post I demonstrated how to use Polymorphism through Inheritance and base instance classes. In this post I demonstrated how to use Inheritance through abstract base classes.

In this post I will show you how to use Polymorphism through Interfaces.

I assume that the people who will read on this post understand basic OOP concepts, like Inheritance and Encapsulation and Interfaces.

The main point to consider is what happens when we want to share a common method among classes but those classes do not fall within the same inheritance tree. In my previous example we used the custom types Animal and Person. But what if we had Person and Auto custom types?We cannot have a base class that those two classes can inherit from.They do not fit in the same class hierarchy tree.

Nevertheless we want both objects to have a DisplayInfo() method.Both of these classes inherit from different base classes. I will use Interfaces in this example to demonstrate how two different types can "subscribe" to the same method through Interfaces. An Interface is a contract and by implementing an interface as class must implement the methods of the interface.

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 IDisplayable.cs.This is the Interface file.We just have the method header of the method. We cannot add implementation in this file.

5) The whole code for the IDisplayable.cs class follows

 

public interface IDisplayable
{
    string DisplayInfo();
}

 

6) Add a new item in your site, a class file.Name the class file Person.cs. This class will implement the IDisplayable interface.

Remember that you can inherit from multiple interfaces but only from a single parent class.Obviously you have to implement the DisplayInfo() method.If you do not do that the compiler will complain.

The whole code for the Person.cs class follows

 public class Person:IDisplayable
{

    #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 + " ,his email is "
 + Email + " ,his Height is: " + Height + " and Weight is " + Weight;
        return info;

}
}

 

7) Add another class file to your site and name it Auto.cs.This class will implement the IDisplayable interface.

Remember that you can inherit from multiple interfaces but only from a single parent class.Obviously you have to implement the DisplayInfo() method.If you do not do that the compiler will complain.

The code inside the .cs file should be something like this

 public class Auto:IDisplayable
{
    
    public string Make { getset; }
    public string Model { getset; }
    public int Year { getset; }
    public int Miles { getset; }
    public double Height { getset; }
    public double Width { getset; }
    public string Color { getset; }

public string DisplayInfo()
{

 string info;
 info = "Specs for my car: " + Make + " " + Model + " " + Year + " " 
 + Miles + " " + Height + " " + Width + " " + Color ;
 return info;

}
}

8) In the Page_Load() event handling routine of the Default.aspx page type

 

 List<IDisplayable> customTypes = new List<IDisplayable>();


        Person p1 = new Person();
        p1.FirstName = "Nikos";
        p1.LastName = "Kantzelis";
        p1.Email = "nikolaosk@hotmail.com";
        p1.Height = 1.78m;
        p1.Weight = 88.5m;


        Person p2 = new Person();
        p2.FirstName = "James";
        p2.LastName = "Rowling";
        p2.Email = "jamesr@hotmail.com";
        p2.Height = 1.98m;
        p2.Weight = 98.25m;

        Person p3 = new Person();
        p3.FirstName = "George";
        p3.LastName = "Graham";
        p3.Email = "graham@yahoo.co.uk";
        p3.Height = 1.88m;
        p3.Weight = 81.5m;


        Auto myAuto = new Auto();

        myAuto.Make = "Fiat";
        myAuto.Model = "Sporting";
        myAuto.Year = 1999;
        myAuto.Miles = 3232;
        myAuto.Height = 1.41;
        myAuto.Width = 2.35;
        myAuto.Color = "Silver";


        Auto myOtherAuto = new Auto();

        myOtherAuto.Make = "BMW";
        myOtherAuto.Model = "Cabrio SL12";
        myOtherAuto.Year = 2009;
        myOtherAuto.Miles = 6232;
        myOtherAuto.Height = 1.71;
        myOtherAuto.Width = 2.85;
        myOtherAuto.Color = "Black";



        customTypes.Add(p1);
        customTypes.Add(p2);
        customTypes.Add(p3);
        customTypes.Add(myAuto);
        customTypes.Add(myOtherAuto);
       

        foreach (IDisplayable item in customTypes)
        {

            Response.Write(item.DisplayInfo());
            Response.Write("<br/>");

        }

 

10) I create a generic collection of type IDisplayable and I call it customTypes.

List<IDisplayable> customTypes = new List<IDisplayable>();

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

Then I create 2 instances of the Auto custom object and fill in the properties with values.

Then I use the Add method to add those objects in my customTypes collection.

Then I just loop through the items in the list and displaying information about them by calling the DisplayInfo() method.


 foreach (IDisplayable item in customTypes)
        {

            Response.Write(item.DisplayInfo());
            Response.Write("<br/>");

        }
11) I can rewrite the foreach loop above like this
 
foreach (var item in customTypes)
        {

            Response.Write(item.DisplayInfo());
            Response.Write("<br/>");

        }
Have a look for the var keyword here. This code works only for c#3. Basically with the var keyword we let the compiler decide what type it is.

12) 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(). 
Note that the classes do not inherit from the same base class but instead they implement the same Interface - IDisplayable.
To recap in this post we demonstratesd how to use Polymorphism with the use of Interfaces.
Email me if you need the source code.
Hope it helps.

Comments

koolll said:

send me the source code pls

my mail is ahkoo888@gmail.com

# August 3, 2011 5:22 AM

Amul said:

Thanks.This is a nice example for polymorphism.

Thanks a lot.

Regards

Amul V Wayangankar

# September 9, 2011 3:42 AM

exlaigacfb@gmail.com said:

I could not resist commenting. Exceptionally well written!

# February 11, 2013 1:24 PM

sarita khaire said:

Very nice e.g of polymorphism ..thanks a lot..

# February 25, 2013 11:53 PM