Inheritance Task in JS

Inheritance Task in JS

Create an Animal class with by default 0 speed and use two methods for run and stop then create another class Rabbit that inherit the animal with function name hide.
class Animal {
    constructor(name) {
      this.speed = 0;
      this.name = name;
    }
    run(speed) {
      this.speed = speed;
      return this.name + " runs with speed " + this.speed;
    }
    stop() {
      this.speed = 0;
      return this.name + " stands still ";
    }
  }
  class Rabbit extends Animal {
    hide() {
      return this.name + "hides";
    }
  }
  let obj = new Rabbit("My animal");
  alert(obj.run(50));
  alert(obj.stop());
  alert(obj.hide());

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*