Polymorphism
Follow along with code examples here!
Table of Contents:
Inheritance and "Is A" Relationships
Inheritance is a pillar of object-oriented programming. It describes a relationship between two classes: a subclass that inherits methods from a superclass. As a result, instances of the sub-class can use methods defined in a super-class.
We call this an "Is A" relationship

Question: What is the inheritance relationship between the Professor class and the Person class? What about the GraduateStudent class and the Person class?
Array is a Subclass of Object
The Array class is a sub-class of the Object class which is the super-class.
Every Array instance gets methods from the Array.prototype which inherits methods from the Object.prototype. Therefore, all arrays can use Object.prototype methods like toString().
Try running the following code:
Establishing Inheritance Between Custom Classes
Imagine we have a Person class. I want to make another class called Programmer that can do everything a Person can, with some additional properties and behaviors that only instances of Programmer will have.
How NOT to do it:
Q: What bad practice exists this code?
Extends and Super
To remove the repetitive code AND to establish a relationship between Programmer and Person, we use the extends and super keywords to define our Programmer class:
Refactor Challenge
Take the first code snippet and refactor it such that the Programmer class extends the Person class.
Using these classes, do the following:
Create an instance of
ProgrammerInvoke all of the methods that the instance inherits
Use the
instanceofoperator to confirm that your instance is aProgrammerAND aPerson
Then, with a partner, discuss these questions:
Question 1: What does extends do?
Question 2: What does super do?
Question 3: What do we know about the relationship between a Programmer and a Person?
Question 4: How does the code method work?
Ben's Answer
extendsmakes theProgrammerinherit methods fromPerson. It setsPerson.prototypeas the prototype forProgrammer.prototypesuper()invokes thePersonconstructor function using its own value ofthis. Any properties that thePersonconstructor sets onthiswill be set onProgrammer.Programmeris said to be a subclass ofPerson.Personis said to be a superclass ofProgrammer.Programmerwill inherit properties and methods fromPersonInstances of
Programmerare also instances ofPersoncodeinvokes thedoActivitymethod inherited fromPerson.prototype
Polymorphism
Polymorphism means "many forms".
Polymorphism is a concept in object-oriented programming where multiple types of objects share "signatures" (they have the same property and method names even if their values/implementations are different).
The impact of polymorphism is that our program can reliably use different types of objects in the same way if they all descend from the same parent class.
This demonstrates polymorphism because ben, reuben, and carmen are all descendants of Person which we know defines a makeFriend method. Even though reuben and carmen are different subtypes, we can treat them as Person objects as well.
A Person can come in "many forms".
Q: What does super.makeFriend(friend) do?
Ben's Answer
super.makeFriend(friend) will call the superclass's makeFriend method, adding the friend argument to the this.friends array and printing out the greeting message. It is common when method overriding to invoke the superclass's version of the method and then adding on additional statements to execute.
Let's look at another example of polymorphism. In this example, we have a Car class and a RaceCar subclass.
Both classes implement a method called drive but they have their own implementations. The code that calls these methods doesn't care how each class implements drive() — as long as instances of Car and RaceCar have a makeSound method at all, the code will work.
The subclass RaceCar uses the Car drive method sandwiched between two of its own console.log statements.
Car objects can come in many forms (they look the same, but they may behave differently).
Using Classes with Other Classes
Challenge: Refactor the makeFriend method so that instead of adding a friend's name, it takes in a Person object. When a person is added as a friend, both person objects should have each other as friends.
Ben's Solution
We only have to modify the Person class and all subclasses will inherit the new behavior. Instead of passing in a friend's name, pass in the entire Person object and have both friends add each other to the friend list.
We have to be careful to not create an infinite recursion. We will end up with a circular reference though.
Challenge: User and Admin
Challenge
Create two classes, User and Admin.
A User should have the following properties:
usernamea string provided to the constructorisOnlinewith a default valuefalse
A User should have the following methods:
loginsetsisOnlinetotrueand prints<username> has logged in!logoutsetsisOnlinetofalseand prints<username> has logged out!
An Admin should be a subclass of User. It should also have:
A property
isAdminset totrueA method called
doSecretAdminStuffthat just prints a message"Doing secret admin stuff".
Then, create a user instance and an admin instance and demonstrate how to use all of their methods.
Summary
Inheritance occurs when a child class inherits properties and methods from a parent class
The
extendskeyword creates this relationshipThe
superkeyword references the parent classYou can invoke
super()to invoke the parent class constructor.
Polymorphism ("many forms") occurs when multiple types of objects share "signatures" (they have the same property and method names).
The impact of polymorphism is that our program can reliably use different types of objects in the same way if they all descend from the same parent class.
Method Overriding means that method signatures are the same even if their implementations are different
Last updated