You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: pages/Classes.md
+7Lines changed: 7 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -71,9 +71,16 @@ tom.move(34);
71
71
```
72
72
73
73
This example covers quite a few of the inheritance features in TypeScript that are common to other languages.
74
+
74
75
Here we see the `extends` keywords used to create a subclass. You can see this where `Horse` and `Snake` subclass the base class `Animal` and gain access to its features.
75
76
77
+
TypeScript has some restrictions about constructors intended to help ensure instances of a class have their properties correctly initialized before the properties are used.
76
78
Derived classes that contain constructor functions must call `super()` which will execute the constructor function on the base class.
79
+
You have to call `super()` before accessing `this` in the derived constructor.
80
+
Both `Horse` and `Snake` technically do that since neither one actually references `this`.
81
+
A `super()` call must also be the first statement in the constructor of a derived class when the derived class contains initialized properties or has parameter properties.
82
+
Neither `Horse` or `Snake`, however, have those kind of properties right now.
83
+
So, as long as those two classes stay that way, they could have console logging or other statements before the `super()` call in their constructors without generating compile-time errors.
77
84
78
85
The example also shows how to override methods in the base class with methods that are specialized for the subclass.
79
86
Here both `Snake` and `Horse` create a `move` method that overrides the `move` from `Animal`, giving it functionality specific to each class.
0 commit comments