Inheritance

  • Learn how inheritance works in JavaScript using prototypes and classes.
  • Introduction to Advanced OOP Concepts

    In the previous lesson, we learned about:

    • Classes

    • Objects

    • Constructors

    Now we move to core OOP pillars that make applications:

    • Reusable

    • Scalable

    • Maintainable

    • Easy to extend

    In this lesson, we will cover:

    1. Inheritance

    2. Encapsulation

    3. Polymorphism

    4. Abstraction (Conceptual)

    Inheritance

    What is Inheritance ?

    Inheritance is an OOP concept where one class acquires the properties and methods of another class.

    In simple words:

    A child class can reuse the code of a parent class.

    Why Do We Need Inheritance ?

    Without inheritance:

    • Code duplication increases

    • Maintenance becomes difficult

    With inheritance:

    • Code reusability

    • Cleaner structure

    • Easy to extend features

    Real-Life Example of Inheritance

    • Parent class: Vehicle

    • Child classes: Car, Bike, Truck

    All vehicles have:

    • Speed

    • Fuel

    But each has different behavior.

    Inheritance in JavaScript

    JavaScript uses the extends keyword to implement inheritance.

    Syntax of Inheritance

    class Parent {

      // properties and methods

    }


    class Child extends Parent {

      // additional properties and methods

    }

Basic Inheritance Example

A child class inherits methods from a parent class and uses both.

class Animal {
  eat() {
    console.log("Animal is eating");
  }
}

class Dog extends Animal {
  bark() {
    console.log("Dog is barking");
  }
}

let dog = new Dog();
dog.eat();   // inherited
dog.bark();  // own method
  • super Keyword

    The super keyword is used to:

    • Call parent class constructor

    • Access parent class methods

Inheritance with Constructor Example

Uses super() to inherit and initialize parent class properties in a child class.

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

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

  display() {
    console.log(this.name, this.rollNo);
  }
}

let s1 = new Student("Rahul", 101);
s1.display();
  • Encapsulation

    What is Encapsulation ?

    Encapsulation means binding data and methods together and controlling access to data.

    In simple terms:

    Protecting internal data from direct access.

    Why Encapsulation Is Important

    • Improves security

    • Prevents accidental modification

    • Makes code easier to maintain

    Encapsulation in JavaScript

    JavaScript achieves encapsulation using:

    • Classes

    • Access control patterns

    • Private fields (modern JS)

Encapsulation with Methods Example

Uses methods to control access and update object data safely.

class BankAccount {
  constructor(balance) {
    this.balance = balance;
  }

  deposit(amount) {
    this.balance += amount;
  }

  getBalance() {
    return this.balance;
  }
}

let account = new BankAccount(1000);
account.deposit(500);
console.log(account.getBalance());
  • Direct modification is avoided by using methods.


Private Fields Example

Uses private fields to restrict direct access to sensitive data.

class User {
  #password;

  constructor(password) {
    this.#password = password;
  }

  checkPassword() {
    return this.#password;
  }
}
  • Private fields cannot be accessed outside the class.

    Polymorphism

    What is Polymorphism ?

    Polymorphism means one method, many forms.

    In simple words:

    The same method name behaves differently in different classes.

    Real-Life Example of Polymorphism

    • draw( ) method:

      • Circle draws a circle

      • Square draws a square

    Same method name, different behavior.

Polymorphism with Overriding Example

Different classes override the same method to show different behavior.

class Shape {
  draw() {
    console.log("Drawing a shape");
  }
}

class Circle extends Shape {
  draw() {
    console.log("Drawing a circle");
  }
}

class Square extends Shape {
  draw() {
    console.log("Drawing a square");
  }
}

let shapes = [new Shape(), new Circle(), new Square()];

shapes.forEach(shape => shape.draw());
  • Why Polymorphism Is Useful

    • Flexible code

    • Easy to extend

    • Reduces conditional logic

    • Supports dynamic behavior

    Abstraction (Conceptual)

    What is Abstraction ?

    Abstraction means hiding internal implementation details and showing only essential features.

    In simple terms:

    Focus on what an object does, not how it does it.

    Real-Life Example of Abstraction

    • ATM machine:

      • User sees: withdraw, deposit

      • User does not see: internal banking logic

    Abstraction in JavaScript

    JavaScript does not have built-in abstract classes like some languages, but abstraction can be achieved using:

    • Base classes

    • Method definitions meant to be overridden

    • Design patterns

Abstraction Example

Defines a common method and forces child classes to implement it.

class Payment {
  pay() {
    throw new Error("Method must be implemented");
  }
}

class CreditCardPayment extends Payment {
  pay() {
    console.log("Payment done using credit card");
  }
}

class UpiPayment extends Payment {
  pay() {
    console.log("Payment done using UPI");
  }
}
  • The base class defines what should be done, not how.

    Comparison of OOP Concepts

    Concept

    Purpose

    Inheritance

    Reuse code

    Encapsulation

    Protect data

    Polymorphism

    Flexible behavior

    Abstraction

    Hide complexity

    Common Mistakes

    • Overusing inheritance

    • Forgetting super() in child constructor

    • Exposing internal data

    • Writing complex inheritance chains

    • Confusing abstraction with encapsulation

    Best Practices for OOP in JavaScript

    • Favor composition over deep inheritance

    • Keep classes focused

    • Use encapsulation wisely

    • Override methods carefully

    • Write readable and maintainable code

    Real-World Applications

    • Banking systems

    • E-commerce platforms

    • Game development

    • User management systems

    • Enterprise applications