Run ❯
Get your
own
website
×
Change Orientation
interface Shape { getArea: () => number; } class Rectangle implements Shape { // using protected for these members allows access from classes that extend from this class, such as Square public constructor(protected readonly width: number, protected readonly height: number) {} public getArea(): number { return this.width * this.height; } public toString(): string { return `Rectangle[width=${this.width}, height=${this.height}]`; } } class Square extends Rectangle { public constructor(width: number) { super(width, width); } // this toString replaces the toString from Rectangle public override toString(): string { return `Square[width=${this.width}]`; } } const mySq = new Square(20); console.log(mySq.toString());
Square[width=20]