Tuesday, June 5, 2018

Simple Polymorphism

What is polymorphism?

According to Wikipedia it "is the provision of a single interface to entities of different types".

According to Techopedia it is a "concept that refers to the ability of a variable, function or object to take on multiple forms".

Basically, polymorphism means we have code that can be reused through the construction of super/base/parent and sub/child classes. That's really it.

My favorite example is the Animal base class. In nature we might say that all (or nearly all, so let's just say all) animals have certain characteristics and abilities. For example, all animals can move, eat, and breathe (again, let's make some assertions we know are not true for the sake of this argument). However, the manner in which all animals do those things varies wildly. If we were to code animals using polymorphism we'd end up with a base class that requires all children to implement certain methods. It might look like this:
   1:  public class Animal
   2:  {
   3:      public virtual string Move()
   4:      {
   5:          return "The animal moves";
   6:      }
   7:  }

This base class defines that an Animal can move, but allows child classes to override the way the animal moves so they can be more specific. Some base classes might look like this:
   1:  public class Horse : Animal
   2:  {
   3:      public override string Move()
   4:      {
   5:          return "The horse trots";
   6:      }
   7:  }

   1:  public class Fish : Animal
   2:  {
   3:      public override string Move()
   4:      {
   5:          return "The fish swims";
   6:      }
   7:  }

   1:  public class Bird : Animal
   2:  {
   3:      public override string Move()
   4:      {
   5:          return "The bird flies";
   6:      }
   7:  }

Now when we want to create an animal we can either create a generic Animal, a Horse, a Fish, or a Bird. No matter which animal we create, we can always invoke the Move method on our instance because we know that every Animal (or derived animal) can Move.
   1:  class Program
   2:  {
   3:      static void Main(string[] args)
   4:      {
   5:          Animal animal = new Animal();
   6:          Console.WriteLine(animal.Move());
   7:          animal = new Horse();
   8:          Console.WriteLine(animal.Move());
   9:          animal = new Fish();
  10:          Console.WriteLine(animal.Move());
  11:          animal = new Bird();
  12:          Console.WriteLine(animal.Move());
  14:      }
  15:  }

What we end up with after all of that is one object (or type) taking many forms. Our Animal object took the form of a Horse, a Bird, and a Fish. Let's look at one more example to get a better idea of why we might do this. We'll create a zoo.
   1:  public class Zoo
   2:  {
   3:      public Zoo(List<Animal> animals)
   4:      {
   5:          Animals = animals;
   6:      }
   7:  
   8:      public List<Animal> Animals { get; set; }
   7:  }
Then we'll populate our zoo:
   1:  class Program
   2:  {
   3:      static void Main(string[] args)
   4:      {
   5:          var zoo = new Zoo(new List<Animal> {new Horse(), new Fish(), new Bird()});
   6:  
   7:          foreach (var animal in zoo.Animals)
   8:          {
   9:              Console.WriteLine(animal.Move());
  10:          }
  11:      }
  12:  }

This is simple polymorphism. A complicated word to describe a relatively simple concept. Objects can take many forms.

No comments:

Post a Comment