Write three classes: Eye, Animal and Dog. A dog is an animal (inheritance). An animal has eyes (composition).
new Eye(color): an eye with the field color (a string). Its method blink() returns 'blink'.new Animal(name, eyeColor): fields name and eyes. eyes is an array of two new Eye objects with the color eyeColor. Every animal gets its own array and its own two Eye objects, and the two eyes are two different objects. The animal creates the eyes once, in the constructor, and keeps them: every read of eyes gives the same array with the same two Eye objects.speak() of Animal returns '<name> makes a sound', for example 'Cat makes a sound'.describe() of Animal returns '<name> has 2 <color> eyes', where <color> is the color of the eyes, for example 'Cat has 2 green eyes'.Dog is a subclass of Animal: new Dog(name, eyeColor, breed) also has the field breed. Dog overrides speak() with its own method that returns '<name> barks'.describe() in Dog: it comes from Animal.Eye does not extend Animal, and Animal does not extend Eye. Check it with instanceof: dog instanceof Animal is true, and dog instanceof Eye is false.new Dog('Rex', 'brown', 'Beagle').speak()Returns 'Rex barks'
new Dog('Rex', 'brown', 'Beagle').describe()Returns 'Rex has 2 brown eyes'
new Animal('Cat', 'green').eyes.lengthReturns 2
Asked at Reltio · Source
Hidden tests run on Submit.