We start the next series of episodes with a polymorphism overview.  This is a core concept for proper object-oriented design.  Likewise, we will dig into several practical ways to use this.


Polymorphism Overview – A Definition

As with many topics, it seems best to start with a definition from Wikipedia.

In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types.

For our purposes, that symbol that is referred to can be considered a name.  The name can be a method, class, or property name.

Class Vs. Object

It is worth clarifying the difference between a class and an object.  We will be talking about these two terms a lot from here on out.  Therefore, let’s remove any confusion.  A Class is the definition of a class.  Think of it as a set of rules or a template.  A simple pseudo-code example is below.

Class MyExample {
  String name;
  String value;

  public whoAmI() {
   return this.name;
  }
}

An object or instance is an occurrence of a class.  In the example below, myPointer is an object (or instance) of the class MyExample.  These are simple examples.  However, the concept is simple.  In the real world, an example would be that there is a class called Mother, and your mom is an instance of that class.

myPointer = new MyExample();

The General Concept And Examples

Since this is a polymorphism overview, we now need to talk about examples.  In general, polymorphism allows us to provide commands to objects they can follow for their specific case.  We have examples of this throughout the real world via commands we give and questions we ask. 

We can look at the request “tell me about yourself” as an example.  The response may be a name, a profession, a season of life, an entire life story, or countless other responses.  In this case, the object the person you are talking to will take that request and polymorphically respond.  It is polymorphic because the same command is understandable by each of us.  However, we will have different responses due to our personality (or class).

In the code world, a command like this may be “save” or “print” or myriad other commands.  These allow us to “tell” a group of objects the same command and have each respond in a pertinent way. 

Rob Broadhead

Rob is a founder of, and frequent contributor to, Develpreneur. This includes the Building Better Developers podcast. He is also a lifetime learner as a developer, designer, and manager of software solutions. Rob is the founder of RB Consulting and has managed to author a book about his family experiences and a few about becoming a better developer. In his free time, he stays busy raising five children (although they have grown into adults). When he has a chance to breathe, he is on the ice playing hockey to relax or working on his ballroom dance skills.

Leave a Reply