Book Image

TypeScript Essentials

By : Christopher Nance
Book Image

TypeScript Essentials

By: Christopher Nance

Overview of this book

Table of Contents (15 chapters)

Understanding inheritance


Inheritance is the ability of a class to extend the functionality of an existing class. Inheritance creates a way for multiple objects to share a common core set of code and then extend or modify this as necessary for a specific purpose. Take for example our Shape class, which is very simple with a couple of properties in it, but these properties don't provide specific details as to what shape is actually being represented. In the following code, you can see we have modified the Shape class to be more generic and created a subclass using the extends construct in TypeScript:

interface IShape {
    location: IPoint;
    move(newLocation: IPoint);
}
class Shape implements IShape {
    public location: IPoint = new Point(0, 0);
    constructor() {
    }
    public move(newLocation: IPoint) {
        this.location = newLocation;
    }
}
interface IRectangle extends IShape {
    height: number;
    width: number;
    area(): number;
    resize(height: number, width: number...