From b932e8074097dd0075a54e47816b2b74f71bf207 Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Panigrahi <2410030162@klh.edu.in> Date: Sun, 16 Nov 2025 01:31:05 +0530 Subject: [PATCH] feat(curriculum): add interactive editors to class inheritance lesson (#63866) --- .../673403d2aa52d8586a14a269.md | 59 +++++++++++++++---- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/curriculum/challenges/english/blocks/lecture-understanding-how-to-work-with-classes-in-javascript/673403d2aa52d8586a14a269.md b/curriculum/challenges/english/blocks/lecture-understanding-how-to-work-with-classes-in-javascript/673403d2aa52d8586a14a269.md index 1f945853975..0942ec1f418 100644 --- a/curriculum/challenges/english/blocks/lecture-understanding-how-to-work-with-classes-in-javascript/673403d2aa52d8586a14a269.md +++ b/curriculum/challenges/english/blocks/lecture-understanding-how-to-work-with-classes-in-javascript/673403d2aa52d8586a14a269.md @@ -4,10 +4,8 @@ title: What Is Class Inheritance, and How Does It Work? challengeType: 19 dashedName: what-is-class-inheritance-and-how-does-it-work --- - -# --description-- - -Let's learn about inheritance and how it works in JavaScript. + +# --interactive-- In programming, inheritance allows you to define classes that inherit properties and methods from other classes. @@ -72,14 +70,32 @@ console.log(myCar.year); myCar.honk(); ``` -Here is the output: +Here is the full example: + +:::interactive_editor ```js -freeCodeCamp Motors -2019 -Honk! Honk! +class Vehicle { + constructor(brand, year) { + this.brand = brand; + this.year = year; + } +} + +class Car extends Vehicle { + honk() { + console.log("Honk! Honk!"); + } +} + +let myCar = new Car("freeCodeCamp Motors", 2019); +console.log(myCar.brand); +console.log(myCar.year); +myCar.honk(); ``` +::: + Indeed, the output is correct, so these properties and method are defined in the `Car` instance and you just saved yourself a lot of code repetition by inheriting these properties from the `Vehicle` class. In this example, the child class didn't have any additional properties. That's why the class didn't have a constructor, only a method. @@ -131,14 +147,33 @@ console.log(myCar.year); console.log(myCar.numDoors); ``` -This is the output: +Here is the full example: + +:::interactive_editor ```js -freeCodeCamp Motors -2019 -4 +class Vehicle { + constructor(brand, year) { + this.brand = brand; + this.year = year; + } +} + +class Car extends Vehicle { + constructor(brand, year, numDoors) { + super(brand, year); + this.numDoors = numDoors; + } +} + +let myCar = new Car("freeCodeCamp Motors", 2019, 4); +console.log(myCar.brand); +console.log(myCar.year); +console.log(myCar.numDoors); ``` +::: + The main advantages of inheritance are code reusability, modularity, extensibility, and improved code structure. By implementing a hierarchy, you can reuse code that you already wrote for a parent class in the child class, avoiding repetition.