Day 3: Understanding JavaScript Classes and Inheritance

Day 3: Understanding JavaScript Classes and Inheritance

Introduction

JavaScript, as a prototype-based language, traditionally used prototypes for object-oriented programming. However, with the introduction of ES6 (ECMAScript 2015), JavaScript now supports a more familiar class-based syntax. This new syntax provides a cleaner and more intuitive way to handle object-oriented programming concepts like classes and inheritance. In this blog post, we will explore JavaScript classes and inheritance, illustrating their syntax, features, and practical use cases.

1. JavaScript Classes

1.1. Syntax

Classes in JavaScript are syntactic sugar over the existing prototype-based inheritance. They offer a clearer and more concise syntax for creating objects and dealing with inheritance.

Basic Class Definition:

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

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

In this example:

  • constructor(): A special method for initializing objects. It’s called when a new instance of the class is created.

  • greet(): A method that can be called on instances of the class.

1.2. Creating Instances

You can create instances of a class using the new keyword.

Example:

const alice = new Person('Alice', 30);
alice.greet(); // Output: Hello, my name is Alice and I am 30 years old.

2. Inheritance

Inheritance allows one class to inherit properties and methods from another class, promoting code reuse and establishing a hierarchy.

2.1. Syntax

In JavaScript, you can create a subclass using the extends keyword.

Example:

class Student extends Person {
  constructor(name, age, studentId) {
    super(name, age); // Call the parent class constructor
    this.studentId = studentId;
  }

  study() {
    console.log(`${this.name} is studying.`);
  }
}

In this example:

  • extends: Indicates that Student is a subclass of Person.

  • super(): Calls the constructor of the parent class (Person), allowing Student to inherit its properties.

2.2. Creating Instances of Subclasses

You can create instances of a subclass in the same way as with a class, and they will inherit methods from the parent class.

Example:

const bob = new Student('Bob', 22, 'S123');
bob.greet();   // Output: Hello, my name is Bob and I am 22 years old.
bob.study();   // Output: Bob is studying.

3. Method Overriding

Subclasses can override methods from the parent class to provide specific implementations.

Example:

class Student extends Person {
  constructor(name, age, studentId) {
    super(name, age);
    this.studentId = studentId;
  }

  greet() {
    console.log(`Hi, I'm ${this.name}, a student with ID ${this.studentId}.`);
  }
}

In this example, the greet method in Student overrides the greet method in Person.

4. Static Methods

Static methods belong to the class itself rather than instances of the class. They are called on the class rather than on instances.

Example:

class MathUtils {
  static add(x, y) {
    return x + y;
  }
}

console.log(MathUtils.add(5, 10)); // Output: 15

5. Practical Use Cases

  • Classes: Use classes to define blueprints for objects with shared properties and methods, enhancing code readability and organization.

  • Inheritance: Utilize inheritance to create specialized subclasses that reuse and extend the functionality of parent classes.

  • Static Methods: Implement static methods for utility functions that are relevant to the class but do not depend on instance-specific data.

Conclusion

JavaScript classes and inheritance bring object-oriented programming concepts to the language in a more accessible and intuitive way. By understanding and applying these features, you can write more structured, maintainable, and reusable code. Embrace the power of ES6 classes to enhance your JavaScript programming skills and build robust applications.


Buy Me A Coffee

Did you find this article valuable?

Support Revive Coding by becoming a sponsor. Any amount is appreciated!