Given classes Animal, Flying_object, I want to instantiate a Bird and Boomerang object according to the following rules :
Boomerangmust inherit fromFlying_objectonly.Birdmust inherit from bothAnimalANDflying_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/