Polymorphism — JS Technical Interview Questions & Answers

Subclasses overriding shared method behaviour.

Polymorphism

  • Polymorphism (js) is a JS topic. In plain words you use it for a short Polymorphism js example in a script in the browser. Don’t start with a slogan — start with that picture.
  • Smallest example: the smallest Polymorphism js example you can type. Type it, run it, and say what you see. If you can do that from memory, you know Polymorphism (js).
  • From the example next to this theory: Subclasses overriding shared method behaviour.
  • Trap: only saying “Polymorphism js” with no example. Fix that before you talk about advanced JS.
  • Viva: what is Polymorphism (js)? Then show the smallest Polymorphism js example you can type. Then name the trap.

Technical Interview Tip: What is Polymorphism (js)? Show this: the smallest Polymorphism js example you can type. Trap: only saying “Polymorphism js” with no example.

Code Examples for Polymorphism

Polymorphism — working example

class Animal {
  static kingdom = "Animalia";
  #secret = "private";
  constructor(name) { this.name = name; }
  speak() { return this.name + " speaks"; }
  reveal() { return this.#secret; }
}
class Dog extends Animal {
  speak() { return super.speak() + " (bark)"; }
}
const d = new Dog("Rex");
console.log(d.speak(), Animal.kingdom, d.reveal());
console.log(Object.getPrototypeOf(d) === Dog.prototype);

Polymorphism — oOP in JS: constructors, prototypes, inheritance, statics, and private fields (#).

How to Explain Polymorphism to an Interviewer

What is Polymorphism (js)?

Used for a short Polymorphism js example a script in the browser Not just the heading

Give one small example of Polymorphism (js).

the smallest Polymorphism js example you can type Subclasses overriding shared method behaviour

What mistake do beginners make with Polymorphism (js)?

only saying “Polymorphism js” with no example Show the fix

Where do you use Polymorphism (js)?

a script in the browser a short Polymorphism js example