I am trying to figure out how to correctly define abstract methods in TypeScript:

Using the original inheritance example:

class Animal {
    constructor(public name) { }
    makeSound(input : string) : string;
    move(meters) {
        alert(this.name + " moved " + meters + "m.");
    }
}

class Snake extends Animal {
    constructor(name) { super(name); }
    makeSound(input : string) : string {
        return "sssss"+input;
    }
    move() {
        alert("Slithering...");
        super.move(5);
    }
}

I would like to know how to correctly define method makeSound, so it is typed and possible to overried.

Also, I am not sure how to define correctly protected methods – it seems to be a keyword, but has no effect and the code won’t compile.

5 Answers
5

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *