Defining Classes and Using Inheritance

Learn ES6 class syntax and object-oriented programming through practical, beginner-friendly examples.

🎯 Introduction

ES6 class syntax helps you organize frontend code using object-oriented programming. This article uses practical examples to explain class definitions, inheritance, methods, and static members. Understanding these features helps you write modular, maintainable code more efficiently.


🚀 What Is an ES6 Class?

JavaScript originally relied on prototype chains rather than traditional class syntax. ES6 introduced classes, providing a way to create objects and express inheritance that is closer to object-oriented languages such as Java and C++.

Class syntax is built on JavaScript's existing prototype-based inheritance, making that model easier to read and understand.


🛠️ Defining a Class

Use the class keyword to define a class:

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    sayHello() {
        console.log(`Hello, I am ${this.name}, and I am ${this.age} years old.`);
    }
}

const person1 = new Person('Alex', 18);
person1.sayHello();

The constructor method runs automatically when an instance is created. sayHello is defined on the prototype, so all instances share that method implementation.

Assign instance properties through this in the constructor so they are accessible on the instance.

🌱 Inheritance and Subclasses

Classes support inheritance: a subclass can inherit properties and methods from a parent class, enabling code reuse.

class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        console.log(`${this.name} makes a sound`);
    }
}

class Dog extends Animal {
    speak() {
        console.log(`${this.name} says woof woof`);
    }
}

const dog = new Dog('Buddy');
dog.speak();

Here, Dog extends Animal and overrides speak. Calling dog.speak() logs the custom "woof woof" message.

Use super to access a parent class method from a subclass. For example:
class Dog extends Animal {
    speak() {
        super.speak();
        console.log(`${this.name} says woof woof`);
    }
}

🔍 Static Methods and Properties

Classes can define static methods and properties, which belong to the class itself rather than its instances.

class MathUtil {
    static add(a, b) {
        return a + b;
    }
}

console.log(MathUtil.add(2, 3)); // 5

Call static methods on the class, not on an instance.


💡 Practical Example: A Simple Counter Component

Let's build a counter that can increment, decrement, and reset its value.

class Counter {
    constructor() {
        this.count = 0;
    }

    increment() {
        this.count++;
        console.log(`Counter incremented; current value: ${this.count}`);
    }

    decrement() {
        this.count--;
        console.log(`Counter decremented; current value: ${this.count}`);
    }

    reset() {
        this.count = 0;
        console.log(`Counter reset; current value: ${this.count}`);
    }
}

const counter = new Counter();
counter.increment();
counter.increment();
counter.decrement();
counter.reset();

The class groups the counter's behavior in a clear structure. This makes Counter well suited to encapsulating component logic.


❓ Knowledge Check (Q&A)

Q: What is a class, and how does it differ from a constructor function?

A: Classes provide ES6 syntax built on prototype-based inheritance. They make code more concise and familiar to developers using traditional object-oriented languages.

Q: How do you call a parent class method from a subclass?

A: Use super to call the parent constructor or its methods.

Q: How do static methods differ from instance methods?

A: Static methods belong to the class itself, while instance methods are accessed through instances. Static methods cannot be called on an instance.


🧪 Exercises:

  1. Add a setCount(value) method to Counter that sets the current counter value.
class Counter {
    constructor() {
        this.count = 0;
    }

    increment() {
        this.count++;
        console.log(`Counter incremented; current value: ${this.count}`);
    }

    decrement() {
        this.count--;
        console.log(`Counter decremented; current value: ${this.count}`);
    }

    reset() {
        this.count = 0;
        console.log(`Counter reset; current value: ${this.count}`);
    }

    // Add the setCount method here
}
  1. Define a Shape class with a color property and a draw() method. Extend it with a Circle class that adds a radius property and implements draw() to log "Draw a red circle with a radius of 10".
class Shape {
    constructor(color) {
        this.color = color;
    }

    draw() {
        console.log(`Draw a shape with the color ${this.color}`);
    }
}

// Extend Shape to implement Circle

🎉 You now understand ES6 classes, inheritance, and static methods! Keep practicing to become comfortable with object-oriented frontend code.