Exercise - Extend a class
In this exercise, you'll extend the Car
class to create a new class called ElectricCar
and override a method.
Continue working in the Playground.
Below the
Car
class, create a new class calledElectricCar
thatextends Car
.class ElectricCar extends Car { // Properties unique to ElectricCar // Constructor // Accessors // Methods }
Declare the property that is unique to the
ElectricCar
class,_range
, as aprivate
property of typenumber
.// Properties private _range: number;
The
constructor
for the subclass is different from theconstructor
for the base class in a few ways.- The parameter list can include any of the properties of both the base class and the subclass. (As with all parameter lists in TypeScript, remember that required parameters must appear before optional parameters.)
- In the body of the
constructor
, you must add thesuper()
keyword to include the parameters from the base class. Thesuper
keyword executes theconstructor
of the base class when it runs. - The
super
keyword must appear before any references tothis.
when referring to properties in the subclass.
Define the class
constructor
forElectricCar
, including the_make
,_color
, and_doors
properties of the base class and the_range
property of the subclass. In thisconstructor
, set the default value of thedoors
parameter to2
.// Constructor constructor(make: string, color: string, range: number, doors = 2) { super(make, color, doors); this._range = range; }
Define the
get
andset
accessors for therange
parameter.// Accessors get range() { return this._range; } set range(range) { this._range = range; }
Enter the following
charge
method that returns a message to the console. This method includes a call to theworker
function that you defined in theCar
class. But it raises the error Property 'worker' is private and only accessible within class 'Car'. Do you know how to correct this problem?// Methods charge() { console.log(this.worker() + " is charging.") }
In the
Car
class, change the access modifier of theworker
function fromprivate
toprotected
. This allows subclasses of theCar
class to use the function, while keeping it hidden from the members available to objects instantiated from the class. The error in thecharge
method should now resolve.Test the new
ElectricCar
class to verify that it's working as expected.let spark = new ElectricCar('Spark Motors','silver', 124, 2); let eCar = new ElectricCar('Electric Car Co.', 'black', 263); console.log(eCar.doors); // returns the default, 2 spark.charge(); // returns "Spark Motors is charging"
Define a new
brake
method in theElectricCar
class that has different implementation details. Note that the parameter signature and return type for thebrake
method must be the same as thebrake
method in theCar
class.// Overrides the brake method of the Car class brake(): string { return `${this.worker()} is braking with the regenerative braking system.` }
Test the new method and verify that it works as expected.
console.log(spark.brake()); // returns "Spark Motors is braking with the regenerative braking system"