Menu
Coddy logo textTech

The "is-a" Relationship

Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 25 of 56.

The "is-a" relationship is a simple test to determine when inheritance makes sense. If you can say "X is a Y" and it sounds correct, then X should probably inherit from Y.

Correct (Use Inheritance):

  • "A Dog is an Animal" → class Dog extends Animal
  • "A Car is a Vehicle" → class Car extends Vehicle
  • "A Textbook is a Book" → class Textbook extends Book

Incorrect (Don't Use Inheritance):

  • "A Driver is a Car" → No, a driver uses a car
  • "A Wheel is a Car" → No, a wheel is part of a car
  • "A Student is a Classroom" → No, a student is in a classroom

For example, let's consider two objects:

// Base class - what they all ARE
class Book {
  constructor(title, author, pages) {
    this.title = title;
    this.author = author;
    this.pages = pages;
  }
  
  read() {
    console.log(`Reading ${this.title} by ${this.author}`);
  }
}
// A Novel IS-A Book (correct inheritance)
class Novel extends Book {
  constructor(title, author, pages, genre) {
    super(title, author, pages);
    this.genre = genre;
  }
}
// Using the classes
const fiction = new Novel("1984", "George Orwell", 328, "Dystopian");
fiction.read();    // "Reading 1984 by George Orwell" - inherited from Book
challenge icon

Challenge

You're given an ElectronicDevice class and a Smartphone class. A Smartphone is a type of ElectronicDevice with additional features. Your task is to create a `myPhone` object with brand "Samsung" and model “Galaxy S23”.

Cheat sheet

The "is-a" relationship is a test to determine when inheritance makes sense. If you can say "X is a Y" and it sounds correct, then X should inherit from Y.

Correct examples (use inheritance):

  • "A Dog is an Animal" → class Dog extends Animal
  • "A Car is a Vehicle" → class Car extends Vehicle
  • "A Textbook is a Book" → class Textbook extends Book

Incorrect examples (don't use inheritance):

  • "A Driver is a Car" → No, a driver uses a car
  • "A Wheel is a Car" → No, a wheel is part of a car
  • "A Student is a Classroom" → No, a student is in a classroom

Example:

// Base class
class Book {
  constructor(title, author, pages) {
    this.title = title;
    this.author = author;
    this.pages = pages;
  }
  
  read() {
    console.log(`Reading ${this.title} by ${this.author}`);
  }
}

// A Novel IS-A Book (correct inheritance)
class Novel extends Book {
  constructor(title, author, pages, genre) {
    super(title, author, pages);
    this.genre = genre;
  }
}

const fiction = new Novel("1984", "George Orwell", 328, "Dystopian");
fiction.read();    // "Reading 1984 by George Orwell" - inherited from Book

Try it yourself

import { ElectronicDevice } from './electronicDevice.js';
import { Smartphone } from './smartphone.js';


// TODO: Create a `myPhone` object with brand "Samsung" and model “Galaxy S23”.


// Test code - don't modify
console.log(myPhone.turnOn());  // Should output "Samsung Galaxy S23 is now ON"
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming