5

The Udacity ES6 training has a question about overriding a base class constructor. I've got a solution but Udacity doesn't let me get away with it.

The assignment is: Create a Bicycle subclass that extends the Vehicle class. The Bicycle subclass should override Vehicle's constructor function by changing the default values for wheels from 4 to 2 and horn from 'beep beep' to 'honk honk'.

class Vehicle {
    constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
        this.color = color;
        this.wheels = wheels;
        this.horn = horn;
    }

    honkHorn() {
        console.log(this.horn);
    }
}

// your code here


/* tests
const myVehicle = new Vehicle();
myVehicle.honkHorn(); // beep beep
const myBike = new Bicycle();
myBike.honkHorn(); // honk honk
*/

The solution that I come up with is:

class Bicycle extends Vehicle {
    constructor(wheels, horn){
        super(wheels, horn)
        this.wheels = 2
        this.horn = "honk honk" 
    }

    honkHorn(){
        super.honkHorn()
    }

}

But that is not good enough And I do not understand why that is. The feedback I got is:

Your Bicycles constructor doesn't set default values for color, wheels, and horn

2
  • 2
    I think the exercice is expecting you to set default values for every constructor arguments even in your Bicycle class. Something like this: jsfiddle.net/nf570m4k Commented Apr 19, 2019 at 10:10
  • extending the above answer (in your Bicycle) constructor(wheels = 2, horn = 'tring tring') Commented Apr 19, 2019 at 10:16

3 Answers 3

5

you should not be using

    this.wheels = 2
    this.horn = "honk honk" 

when already overriding these in super constructor.

class Vehicle {
	constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
		this.color = color;
		this.wheels = wheels;
		this.horn = horn;
	}

	honkHorn() {
		console.log(this.horn);
	}
}

class Bicycle extends Vehicle {
	constructor(wheels = 2, horn = 'honk honk') {
		super(undefined, wheels, horn);
	}

	honkHorn() {
		super.honkHorn()
	}

}

let by = new Bicycle();
by.honkHorn();

Sign up to request clarification or add additional context in comments.

2 Comments

have you tried removing ` this.wheels = 2 this.horn = "honk honk" ` from Bycible constructor. @Edwin
You should also not set color to be undefined. It will not allow you to submit with color set to undefined. Just use: class Bicycle extends Vehicle { constructor(color, wheels = 2, horn = "honk honk") { super(color, wheels, horn); }
0
class Bicycle extends Vehicle {
    constructor(wheels =2, horn= "honk honk"){
        super(undefined, wheels, horn)
    }

    honkHorn(){
        super.honkHorn()
    }

}

then for the test I added:

const yourBike = new Bicycle(3, "tring tring")

Although other options did provide the right answers for the test cases described in the question. By adding this extra test I found out that overriding a base class constructor is not possible from super nor from this.wheels (as was my first attempt).

However Udacity does not accept it......

Comments

0
class Vehicle {
    constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
        this.color = color;
        this.wheels = wheels;
        this.horn = horn;
    }

    honkHorn() {
        console.log(this.horn);
    }
}


class Bicycle extends Vehicle {
    constructor(color,wheels = 2,horn = 'honk honk'){
        super(color,wheels,horn);
    }
}

you need not want to call honkHorn() in the child, it automatically calls from the parent.

const myVehicle = new Vehicle();
myVehicle.honkHorn(); // beep beep
const myBike = new Bicycle();
myBike.honkHorn(); // honk honk

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.