OOP Concepts — Polymorphism

Elijah Wines
3 min readNov 29, 2022

In today’s article we will be discussing Polymorphism, one of the 4 pillars of Object Oriented Programming. We will be taking a look at what Polymorphism is, how and why we use it, and a coding example using JavaScript. Before we dive in, let’s talk a bit about Object Oriented Programming.

Object Oriented Programming

Object Oriented Programming is a model that organizes software design around objects. It allows us to focus on the objects that we want to manipulate, rather than the functions we use to manipulate them. This is very useful when creating large applications that are complex and actively maintained by developers. OOP is broken down into 4 key components: Classes, Objects, Methods, and Attributes. Together, these components can successfully work together to create code that is well-structured, easy to read, and reusable.

There are 4 pillars that hold up Object Oriented Programming that you should always keep in mind when following the OOP model. Today, we will be discussing Polymorphism, which is one of those 4 pillars.

Polymorphism

What is Polymorphism?

Polymorphism is a pattern we follow that designs objects to share behaviors, but more importantly, that also allows us to be able to override these shared behaviors with more specific ones. Using Polymorphism, we can create methods that can perform different tasks based on the class of the object that calls it. This way, each object can respond appropriately based on the properties of that class.

Why use it?

We use Polymorphism so we can have multiple forms of various objects, variables, and methods. There can even be different implementations of the same method that is specifically catered to the class.

How to use Polymorphism

Inheritance, which is actually another one of the 4 pillars of OOP, is a great way to utilize Polymorphism. You can define a method in a parent class that performs a generic task, then alter the same method slightly in a child class to fit that classes need. This utilizes the concepts of method overriding, overloading, and virtual functions.

What are the benefits of using Polymorphism?

  • Code is more reusable
  • Can have a single variable name for multiple data types
  • Coupling is reduced between different functionalities

Example

Let’s talk about animals! A very easy example to explain Polymorphism is through animals. A dog and a bird are both animals, correct? But even though they are animals, they are very different, and interact with the world in a different way.

In our example, we will create a Class, Animal. Inside this class we will create a method called speak. We will also create two more Classes, Dog and Bird, that will also have a method called speak, but the methods in these classes will provide a different output. Let’s start coding!

class Animal {
speak() {
console.log('** makes animal noises **')
}
}

Here is our Animal Class. Inside is our speak method that simply logs a string to the console.

class Bird extends Animal {
speak() {
console.log('tweet tweet')
}
}

class Dog extends Animal {
speak() {
console.log('woof woof')
}
}

Here, we have our Dog and Bird Classes, each with their own speak method. We are overriding the speak method of the Animal Class so the Dog and Bird Classes produce a unique output based on the Class itself.

var animal = new Animal();
var dog = new Dog();
var bird = new Bird();

animal.speak();
// ** makes animal noises **
dog.speak();
// woof woof
bird.speak();
// tweet tweet

Here, we are creating an instance of each of the created Classes. We then call the speak method on each variable. Even though we are calling the same method, they all produce a different output. This is the power of Polymorphism!

Conclusion

In this article, we talked about the Object Oriented Programming concept of Polymorphism. We talked about what it is, how and why we use it, its benefits, and also an example using JavaScript. Overall, This technique is extremely valuable in the programming world. Being able to understand it and utilize it will make your coding projects much more readable, reusable, and powerful! I hope you learned something from this article, and be sure to check out my other articles here on medium as well! Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Elijah Wines
Elijah Wines

Written by Elijah Wines

Software Engineer - JavaScript, Ruby, React, Ruby on Rails

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations