Back to questions
JuniorTheoryCommon
What does the program print?
class Animal {
describe(): string {
return "animal";
}
}
class Dog extends Animal {
override describe(): string {
return `dog ${super.describe()}`;
}
}
const pet: Animal = new Dog();
console.log(pet.describe());dog animal
- Expecting the variable's type
Animalto pick the method: the type only limits what tsc lets you call, and at run time the object is aDog, so itsdescriberuns. - Expecting
overrideto remove the base method:super.describe()still reaches the version ofAnimal, which returnsanimal.
- What would the program print if
Dogdid not overridedescribe? - What does tsc report if
overrideis written on a method the base class does not have?