0

I'm trying to figure out the correct use of inheritance. In the code below I'm trying to instantiate my ReadingMaterial class, and make an array of books and magazines, inheriting from ReadingMaterial. Am I using inheritance correctly here?

// Define our class with constructor
class ReadingMaterial {
    constructor(author, type, price, title){
        this.author = author;
        this.type = type;
        this.price = price;
        this.title = title;
    }
}
// variable books containing 2 different books
var books = [
    new ReadingMaterial("Lars Tvede", "Fagliteratur", "399kr","Supertrends"),
    new ReadingMaterial("Rasmmus Tantholdt", "Faglitteratur","249kr","Med åbne øjne"),
    new ReadingMaterial("Jussi Alder", "Skønlitteratur", "349kr","Offer 2117")
];
// feature that allows you to type `showMagazines();` in the console
// so we can see our magazines
function showBooks (){
    console.log(books);
};
// variable magazine containing 2 different magazines
var magazine = [
    new ReadingMaterial("Euroman", "Magasin", "99kr","Mick Øgendahl"),
    new ReadingMaterial("Alt for damerne", "Magasin", "149kr","Boligindretning")
];
// feature that allows you to type `showMagazines();` in the console
// so we can see our magazines
function showMagazines() {
    console.log(magazine);
};
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • 8
    I see no inheritance here, just two arrays of ReadingMaterial objects. There's no behavior unique to either book or magazine. – duffymo Jan 09 '20 at 12:25
  • 2
    You are. You have only one class here, nothing is inherited. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance – Roberto Zvjerković Jan 09 '20 at 12:26
  • 3
    Well, the individual `new ReadingMaterial` instances of course do inherit from `ReadingMaterial.prototype`, but you're not using that anywhere. – Bergi Jan 09 '20 at 12:35
  • 3
    I think you've confused instantiation with inheritance. – Dexygen Jan 09 '20 at 12:42

1 Answers1

1

Inheritance occurs when you have two entities in which one derives from the other. For example, when you inherit eye color from one of your parents, then you both have blue eyes.

Please take a look at this example (source):

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

  run() {
    console.log(`Runs on ${this.legs} legs`);
  }
}

class Dog extends Animal {
  bark() {
    console.log("Woof!");
  }
}

const d = new Dog(4);
d.run(); // <- uses `run` method from `Animal`
d.bark(); // <- uses its own method

In your example, you could create a ReadingMaterial class and then classes like Magazine and Book that would inherit something from the ReadingMaterial.

Inheritance is very vast and sometimes complex concept. It's very easy to overuse it and extend classes too deep (for example, Animal -> Dog -> Husky -> AlaskanHusky) and loose track of what is actually going on.

I would suggest getting more info about composition over inheritance.

Tomek Buszewski
  • 7,659
  • 14
  • 67
  • 112