2018-09-30 23:01:58 +01:00
---
id: 587d7db1367417b2b2512b88
title: Override Inherited Methods
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301322
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-07-18 17:32:12 +02:00
2018-09-30 23:01:58 +01:00
In previous lessons, you learned that an object can inherit its behavior (methods) from another object by cloning its < code > prototype< / code > object:
2019-05-17 06:20:30 -07:00
```js
ChildObject.prototype = Object.create(ParentObject.prototype);
```
2018-09-30 23:01:58 +01:00
Then the < code > ChildObject< / code > received its own methods by chaining them onto its < code > prototype< / code > :
2019-05-17 06:20:30 -07:00
```js
ChildObject.prototype.methodName = function() {...};
```
2018-09-30 23:01:58 +01:00
It's possible to override an inherited method. It's done the same way - by adding a method to < code > ChildObject.prototype< / code > using the same method name as the one to override.
Here's an example of < code > Bird< / code > overriding the < code > eat()< / code > method inherited from < code > Animal< / code > :
2019-05-17 06:20:30 -07:00
```js
function Animal() { }
Animal.prototype.eat = function() {
return "nom nom nom";
};
function Bird() { }
// Inherit all methods from Animal
Bird.prototype = Object.create(Animal.prototype);
// Bird.eat() overrides Animal.eat()
Bird.prototype.eat = function() {
return "peck peck peck";
};
```
2018-09-30 23:01:58 +01:00
If you have an instance < code > let duck = new Bird();< / code > and you call < code > duck.eat()< / code > , this is how JavaScript looks for the method on < code > duck’ s< / code > < code > prototype< / code > chain:
2019-07-18 17:32:12 +02:00
2018-09-30 23:01:58 +01:00
1. duck => Is eat() defined here? No.
2. Bird => Is eat() defined here? => Yes. Execute it and stop searching.
3. Animal => eat() is also defined, but JavaScript stopped searching before reaching this level.
4. Object => JavaScript stopped searching before reaching this level.
2019-07-18 17:32:12 +02:00
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-07-18 17:32:12 +02:00
2018-09-30 23:01:58 +01:00
Override the < code > fly()< / code > method for < code > Penguin< / code > so that it returns "Alas, this is a flightless bird."
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: < code > penguin.fly()</ code > should return the string "Alas, this is a flightless bird."
2019-07-24 02:32:04 -07:00
testString: assert(penguin.fly() === "Alas, this is a flightless bird.");
2018-10-04 14:37:37 +01:00
- text: The < code > bird.fly()</ code > method should return "I am flying!"
2019-07-24 02:32:04 -07:00
testString: assert((new Bird()).fly() === "I am flying!");
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function Bird() { }
Bird.prototype.fly = function() { return "I am flying!"; };
function Penguin() { }
Penguin.prototype = Object.create(Bird.prototype);
Penguin.prototype.constructor = Penguin;
// Add your code below this line
// Add your code above this line
let penguin = new Penguin();
console.log(penguin.fly());
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function Bird() { }
Bird.prototype.fly = function() { return "I am flying!"; };
function Penguin() { }
Penguin.prototype = Object.create(Bird.prototype);
Penguin.prototype.constructor = Penguin;
Penguin.prototype.fly = () => 'Alas, this is a flightless bird.';
let penguin = new Penguin();
console.log(penguin.fly());
```
< / section >