Polymorphism

I've talked a bit about polymorphism before, but it's a very cool thing, when you apply it with something called a FACTORY.

Basically, a Factory is a big IF-Statement, that directs an object to do something, depending on what it really is.   A factory is a subset of a programming concept called a "Design Pattern."   More, hopefully, on that later.

Polymorphism is the unique ability of an object oriented language to have objects be more than one type of "thing."   It really is a real-world application of INHERITANCE.

So, consider the example of a Human Being.   A human being is a mammal.   So is a dog.   So is a cat.   There are common attributes that mammals share, which means that if we were to represent humans, dogs, cats, etc in a computer program, we could use inheritance to do it.

So, let's just throw out some code to do just that:


class Mammal
{
   ///Create Some Private Variables here
   private bool _hasHair, _givesBirthToLiveYoung;

   ///Expose some attributes here
   ///I make them protected
   protected bool HasHair
   {
      get { return _hasHair; }
      set { _hasHair = value; }
   }

   protected bool GivesBirthToLiveYoung
   {
      get { return __givesBirthToLiveYoung; }
      set { __givesBirthToLiveYoung = value; }
   }
}

class Human : Mammal
{
    private bool _walksUpright;
    public bool WalksUpright
    {
         get { return _walksUpright; }
         set { _walksUpright = value; }
    }
}

class Dog : Mammal
{
////Let's assume I put some attributes and behaviors here
}

class Cat : Mammal
{
////Let's assume I put some attributes and behaviors here
}


Now, in the above classes, Human, Dog, and Cat all inherit from Mammal the Gives Birth To Live Young and Has hair attributes.   Obviously there's a lot more that a mammal could give to our child-classes, but it's not necessary to go into all that.

I may have a factory class that has a method in it that takes a parameter of type Mammal, and then does something with the object, depending on if it is a Human, Dog, or Cat...or better yet, that method could take a list variable that has a bunch of Mammal objects in it (really the only reason I'm doing that here is to better describe polymorphism).

So consider my factory below:


public void DoWork(List inputMammalList)
{
    foreach(Mammal animal in inputMammalList)
    {
          if(animal is Human)
          {
               ///Do Human stuff
          }
          else if(animal is Dog)
          {
               ///Do Dog stuff
          }
          else if(animal is Cat)
          {
               ///Do Cat stuff
          }

    }
}


Polymorphism is very useful because it really allows you to apply a lot of concepts of object oriented programming.

One final note:   Some people favor the use of interfaces over inheritance (myself included).   The same sort of thing could be applied if mammal were an interface, and were implemented by Human, Dog, and Cat; of course, the difference would be that the interface mammal would have no real implementation.   It would only contain the names of the usable properties and behaviors the Human, Dog, and Cat classes could implement.
Next Recommended Reading Types of Polymorphism in OOP