DEV Community

t-suarez
t-suarez

Posted on

Polymorphism

Inheritance

Polymorphism in Object-Oriented Programming (OOP) is the concept of supplying an interface to elements of different types, with which the program can interact with elements that inherit methods from the interface. This idea has been taken from the biological term polymorphism which refers to the principle in which an organism or species can have many different forms or stages. The concept in computer science allows for functionality to be passed down via inheritance to classes that extend the prototype or abstract class/interface.

Here we are creating an animal class:

class Animal{
  constructor(name){
    this.name = name;
  }
  speak(){
    console.log('I am an animal');
  }
  eat(){
    console.log('Yum!');
  }
}
Enter fullscreen mode Exit fullscreen mode

Inside of this Animal class we have declared a method speak() that takes no parameters and prints a string of text to the console. With Javascript ES6 we use the class keyword that injects some code behind the scenes at compile time that easily enables inheritance for us and cleans up the code. Next we are going to define some other classes that will inherit from our class Animal.

class Dog extends Animal{
  constructor(name){
    super(name);
  }
  speak(){
    console.log("Bark!");
  }
}
Enter fullscreen mode Exit fullscreen mode

Now we've created a Dog class that extends class Animal. This means that Dog inherits functionality that was defined from within its declared superclass. We've redefined the speak() method in such a way that it makes sense for the Dog class, in computer science this is known as "method overriding". Within the Animal class we defined a functionality (method speak()) which was subsequently inherited by class Dog and ensures that any Dog would at least have the default behavior that was defined in the Animal superclass. We are in essence using the Animal class as a classic interface that we can use to produce expected behaviors from its subclasses. Let's take another look at a subclass example:

class Cat extends Animal{
  constructor(name){
    super(name);
  }
  speak(){
    console.log("meow...");
  }
}
Enter fullscreen mode Exit fullscreen mode

Like class Dog before it, Cat inherits predetermined functionality from Animal—the same method defined in Animal has been translated to its descendant subclasses. Once again, the speak() class has been overridden to a definition that is fitting for our new kitty.

We can now interface with our new classes in a manner defined by our original Animal class and we can guarantee either default behavior through inherited methods, or custom actions through method overriding.

let dog = new Dog('Dan');
let kitty = new Cat('Kevin');
dog.speak();    //prints "Bark!"
kitty.speak();  //prints "meow..."
Enter fullscreen mode Exit fullscreen mode

Conclusion

Though the concept of polymorphism we can guarantee inherited behavior to subclasses that extend a superclass, and with method overriding we can customize that behavior such that the superclass becomes an effective interface that our programs can use to elicit actions through predetermined method declarations. Thank you for taking the time to read this and I hope that you learned something useful. Have a nice day.

Latest comments (0)