Any conflicts on this super class prototype?

Does a super class create a single prototype for ALL subclass instances? I hope not and don’t think so based on the behavior of getWheels() below:


class Vehicle {
  constructor(make, model, wheels) {
    this.make = make;
    this.model = model;
    this.wheels = wheels;
  }
  
  getWheels() {
    return this.wheels;
  }
  
  info() {
    return `${this.make} ${this.model}`
  }
}

class Car extends Vehicle {
  constructor(make, model) {
    super(make, model, 4)
  }
}

class Motorcycle extends Vehicle {
  constructor(make, model) {
    super(make, model, 2)
  }
}

class Truck extends Vehicle {
  constructor(make, model, payload) {
    super(make, model, 6);
    this.payload = payload;
  }
}

const car = new Car("Ford", "Bronco");
const mc = new Motorcycle("Kawasaki", "Ninja");
const mcWheels = mc.getWheels();

console.log(car.getWheels()); // 4
console.log(mc.getWheels()); // 2
1 Like

Yes. that’s the point. Unless you override the method with a different implementation, it uses the one in the prototype.

I think you’re talking about the properties stored in the instance, they aren’t stored on the prototype. Again, that’s the point. You are creating an object with some specific values set, it wouldn’t be useful to those values shared, otherwise how do you have more than one instance?

1 Like

One super class can’t have multiple prototype! is this statement right?