0

Given classes Animal, Flying_object, I want to instantiate a Bird and Boomerang object according to the following rules :

  • Boomerang must inherit from Flying_object only.
  • Bird must inherit from both Animal AND flying_object.

Problem: I can't seem to find the proper way to instantiate Bird as both an Animal and a Flying_object.

I can't make Animal inherit from Flying_object because not all animals are flying objects, neither can I make Flying_object inherit from Animal, because a boomerang is not an animal.

/**
 * Only Bird must inherit this class
*/
var Animal = function() {
    this.className = 'Animal';
};
Animal.prototype.sayClassName = function() {
    console.log('I am a '+ this.className);
};

/**
 * Both Boomerang and Bird must inherit this class
*/
var Flying_object = function() {
    this.className = 'Flying_object';
};
Flying_object.prototype.sayName = function() {
    console.log('I am a '+ this.className);
};
Flying_object.prototype.fly = function() {
    console.log(this.className +' can fly');
}

Error trying to instantiate Bird as both Animal and Flying_object:

var Bird = function() {
    this.className = 'Bird';
}
Bird.prototype = new Animal();        //----> Bird should inherit from Animal
Bird.prototype = new Flying_object(); //----> It should also inherit from Flying_object
Bird.prototype.sayName = function() {
    console.log('I am a '+ this.className);
};

/**
 * instantiating...    
*/
var bird = new Bird();
bird.sayName();
bird.fly();
bird.isAnimal(); //---> Uncaught TypeError: bird.isAnimal is not a function

I understand why I get this error: Bird.prototype = new Animal(); is overloaded by Bird.prototype = new Flying_object(); so it really only inherits from Flying_object. Problem is I have no idea how to solve this issue.

Is there any way I can make Bird inherit from BOTH Animal and Flying_object ? If not, what would be the proper approach to make bird share all the properties and methods from Animal and Flying_object ?

JSfiddle: https://jsfiddle.net/Hal_9100/9hn30pdz/

Hal_9100
  • 773
  • 1
  • 7
  • 17
  • 1
    That's not how inheritance, in any language, works. – Adam Jenkins Nov 20 '17 at 14:11
  • 1
    @Adam: Well, there are some [multi-inheritance](https://en.wikipedia.org/wiki/Multiple_inheritance) languages (C++, for instance). JavaScript just isn't one of them, but mixins and/or composition are usually good enough. – T.J. Crowder Nov 20 '17 at 14:14
  • @Adam: `Bird.prototype = new Animal();` is actually classical inheritance (although JS doesn't handle inheritance like other OOP languages). My attempt to make `Bird` inherit from both `Animal` and `Flying_object` is indeed not the way multi inheritance works, which is the reason I'm asking how to achieve this in JavaScript – Hal_9100 Nov 20 '17 at 14:21
  • @T.H. Crowder So if I'm understanding the answer you linked as a duplicate correctly, my only option would be to do something like `Bird.prototype = new Animal(); Bird.prototype.fly_methods = new Flying_object();` and then access it this way: `var bird = new Bird(); bird.fly_methods.fly();`. Am I correct ? – Hal_9100 Nov 20 '17 at 14:25

0 Answers0