# Classes: Syntax & Inheritance

> Organize your code using object-oriented blueprints. Learn how to construct instances and share logic using ES6 class syntax.

Subject: JavaScript | Lesson 23 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/classes
Previous: [The Prototype Chain](https://codewithanimation.in/javascript/prototype-chain.md) | Next: [Private Fields, Getters & Static](https://codewithanimation.in/javascript/private-fields-and-static.md)

## What is a Class?

A `class` in JavaScript is a blueprint for creating objects. Introduced in ES6, it provides a much cleaner, more familiar syntax for object-oriented programming compared to older prototype-based constructor functions.

A class groups related data (properties) and behavior (methods) together into a single, reusable template.

## The Constructor and 'new'

Every class has a special `constructor()` method. This function runs automatically whenever you create a new instance of the class.

To create an instance, you must use the `new` keyword, like `const hero = new Player('Link')`. The `new` keyword creates a fresh object, binds `this` to that new object, and returns it.

## Inheritance (extends & super)

You can create a new class based on an existing one using the `extends` keyword. For example, `class Warrior extends Player`.

The child class inherits all methods from the parent. However, if the child class has its own `constructor`, it MUST call `super()` before it is allowed to use the `this` keyword. `super()` executes the parent's constructor first.

## Common mistake: Missing 'new' throws an error

If you try to call a class like a normal function (e.g., `const hero = Player()`), JavaScript will immediately throw a `TypeError`. Classes strictly require the `new` keyword to be instantiated.

## Tip: It's just syntactic sugar

Under the hood, JavaScript doesn't have true classes like Java or C#. ES6 classes are just 'syntactic sugar' over JavaScript's existing Prototypal Inheritance system. When you use `extends`, you are just wiring up the `__proto__` chain!

## Try it: The Class Factory

Step through the code to see how the `new` keyword builds an instance, and how `extends` and `super` link a subclass to its parent.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/classes

## Check yourself

### Question 1

What happens if a child class with a constructor forgets to call `super()`?

- A. JavaScript throws a ReferenceError when you try to access 'this'.
- B. It silently skips the parent constructor.
- C. It automatically calls super() for you.

Correct answer: A

In a derived class (using `extends`), you are not allowed to use `this` until `super()` has been called to initialize the parent portion of the object.

- Why not B: Incorrect. JavaScript enforces that the parent must be initialized first.
- Why not C: Incorrect. You must call it explicitly if you define a constructor.

### Question 2

What does the `new` keyword do?

- A. It creates a new object, points 'this' to it, runs the constructor, and returns the object.
- B. It copies the class definition into a variable.
- C. It dynamically imports a module.

Correct answer: A

The `new` keyword is the engine of instantiation. It handles all the boilerplate of creating the object, linking its prototype, and returning it.

- Why not B: Incorrect. It builds a concrete instance based on the class blueprint.
- Why not C: Incorrect. Dynamic imports use `import()`, not `new`.

### Question 3

Are JavaScript classes exactly the same as classes in languages like Java or C++?

- A. No, they are 'syntactic sugar' over JavaScript's prototype chain.
- B. Yes, they are fundamentally identical.
- C. No, because JS classes cannot have methods.

Correct answer: A

While the syntax looks like classical OOP, JavaScript still uses prototypes under the hood. The `class` keyword just makes setting up the prototype chain much easier.

- Why not B: Incorrect. JS uses prototypal delegation, not classical memory layouts.
- Why not C: Incorrect. JS classes absolutely have methods.

## Remember this

- A `class` is a blueprint for creating objects.
- The `constructor()` runs automatically when you use the `new` keyword.
- The `new` keyword creates the object and binds `this`.
- `extends` creates a subclass that inherits from a parent.
- You must call `super()` in a subclass constructor before accessing `this`.
