Polymorphism

Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

Polymorphism, meaning many forms, allows objects of different classes to be treated as objects of a common type (usually a superclass).

The ability to treat objects of different types in a similar manner. Example: Giraffe and Crocodile are both Animals, and animals can move(). If you have an instance of an Animal, then you can call move() without knowing or caring what type of animal it is.

Polymorphism is an approach to expressing common behavior between types of objects that have similar traits. It also allows for variations of those traits to be created through overriding.

  1. It enables a single interface to be used for different underlying implementations.
  2. Lets us perform a task in multiple forms or ways.
  3. It is applied to the functions or methods.
  4. It allows the object to decide which form of the function to implement at compile-time as well as run-time.
  5. For example, a method called move() can be implemented differently in a Dog class and a Bird class, but both can be called using the same move() method.

Types of Polymorphism

  1. Compile-time polymorphism (Method overloading)
  2. Run-time polymorphism (Method Overriding)

Mechanics

The method to be invoked is determined at runtime based on the type of the object. This is a situation that results when you have one class inheriting from another and overriding a particular method.

Benefits

Suppose you work for a company that sells pens. So you make a very nice class called Pen that handles everything that you need to know about a pen. You write all sorts of classes for billing, shipping, creating invoices, all using the Pen class. A day boss comes and says, Great news! The company is growing and we are selling Books & CD's now! Not great news because now you have to change every class that uses Pen to also use Book & CD. But what if you had originally created an interface called SellableProduct, and Pen implemented this interface. Then you could have written all your shipping, invoicing, etc classes to use that interface instead of Pen. Now all you would have to do is create a new class called Book & CompactDisc which implements the SellableProduct interface. Because of polymorphism, all of the other classes could continue to work without change!

How can we achieve Polymorphism?

  1. Using Inheritance
    1. extends
    2. Provides polymorphism and code reuse
    3. This is a way to achieve polymorphism through an object hierarchy where objects express relationships and abstract behaviors
  2. Interfaces
    1. They provide only polymorphism but no code reuse
    2. Preferable when the implementations are quite different
      1. e.g. move() for a Dog and move() for a Lizard can be quite different.
  3. Using Prototypes
    1. Prototype is another way to express polymorphism that is different from inheritance.
    2. JavaScript is an example of a language that uses prototype.
  4. In dynamic languages, polymorphism can be achieved with Duck Typing
    1. the classes don’t even need to share the same base class or interface, they just need a method with the same name.
    2. In Javascript, you don’t even need classes at all, just an object with the same method name can be used polymorphically.

Tags

  1. Interfaces and Abstract classes
  2. Dynamic Method Dispatch or Late Binding or Runtime Polymorphism in Java
  3. Overriding vs Overloading in Java