Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Shape Inheritance

Object-Oriented Programmingmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 226: ES6 Classes

Define a base `Shape` class with `Square` and `Rect` subclasses overriding `area()`. Implement `shapeAreas(shapes)` mapping each shape descriptor to its area. A square has `{ type: 'square', side }`; a rectangle has `{ type: 'rect', w, h }`.

Sample tests

Input: shapeAreas([{"type":"square","side":2},{"type":"rect","w":2,"h":3}])
Output: [4,6]
Input: shapeAreas([{"type":"square","side":3}])
Output: [9]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting super() in a subclass constructor throws.
  • Not overriding area() in a subclass.

Learning resources

  • MDN: Classes
Visualize this concept: ES6 Classes →
Approach & explanation (try first)

Subclasses override area() and the mapper picks the right class per descriptor, demonstrating polymorphism.

Loading...
⌘/Ctrl + Enter

Run your code to see results.