Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit d0fa464

Browse files
committed
updates based on PR feedback
1 parent 7775cb5 commit d0fa464

1 file changed

Lines changed: 42 additions & 27 deletions

File tree

pages/Classes.md

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ The example also shows how to override methods in the base class with methods th
7777
Here both `Snake` and `Horse` create a `move` method that overrides the `move` from `Animal`, giving it functionality specific to each class.
7878
Note that even though `tom` is declared as an `Animal`, since its value is a `Horse`, when `tom.move(34)` calls the overriding method in `Horse`:
7979

80+
Derived classes that contain constructor functions must call `super()` which will execute the constructor function on the base class.
81+
8082
```
8183
Slithering...
8284
Sammy the Python moved 5m.
@@ -292,46 +294,59 @@ console.log(grid2.calculateDistanceFromOrigin({x: 10, y: 10}));
292294
```
293295
# Abstract Classes
294296

295-
Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly.
296-
They operate similar to abstract classes in other popular languages such as C# and Java. Unlike an interface, an
297-
abstract class may contain implementation details for it's members. The `abstract` keyword is used to define
298-
abstract classes as well as abstract methods within an abstract class.
297+
Abstract classes are base classes from which other classes may be derived.
298+
They may not be instantiated directly.
299+
Unlike an interface, an abstract class may contain implementation details for its members.
300+
The `abstract` keyword is used to define abstract classes as well as abstract methods within an abstract class.
299301

300-
Methods within an abstract class that are marked as abstract do not contain an implementation and must be implemented
301-
in derived classes.
302+
```TypeScript
303+
abstract class Animal {
304+
abstract makeSound(): void;
305+
move(): void {
306+
console.log('roaming the earth...');
307+
}
308+
}
309+
```
302310

303-
Classes derived from abstract classes that also contain constructor functions must call `super()` which will execute
304-
the constructor function on the abstract base class.
311+
Methods within an abstract class that are marked as abstract do not contain an implementation and must be implemented in derived classes.
312+
Abstract methods share a similar syntax to interface methods.
313+
Both define the signature of a method without including a method body.
314+
However, abstract methods must include the `abstract` keyword and may optionally contain access modifiers.
305315

306316
```TypeScript
307-
abstract class DepartmentBase {
317+
abstract class Department {
308318

309-
constructor(public name: string) {
310-
}
319+
constructor(public name: string) {
320+
}
311321

312-
PrintName(): void {
313-
console.log('Department name: ' + this.name);
314-
}
322+
printName(): void {
323+
console.log('Department name: ' + this.name);
324+
}
315325

316-
abstract PrintMeeting(): void; // must be implemented in derived classes
326+
abstract printMeeting(): void; // must be implemented in derived classes
317327
}
318328

319-
class AccountingDepartment extends DepartmentBase {
329+
class AccountingDepartment extends Department {
330+
331+
constructor() {
332+
super('Accounting and Auditing'); // constructors in derived classes must call super()
333+
}
320334

321-
constructor() {
322-
super('Accounting and Auditing'); // constructors in derived classes must call super()
323-
}
335+
printMeeting(): void {
336+
console.log('The Accounting Department meets each Monday at 10am.');
337+
}
324338

325-
PrintMeeting(): void {
326-
console.log('The Accounting Department meets each Monday at 10am.');
327-
}
339+
generateReports(): void {
340+
console.log('Generating accounting reports...');
341+
}
328342
}
329343

330-
let dept: DepartmentBase = new DepartmentBase(); // error: cannot create an instance of an abstract class
331-
332-
let acctDept: AccountingDepartment = new AccountingDepartment();
333-
acctDept.PrintName();
334-
acctDept.PrintMeeting();
344+
let department: Department; // ok to create a reference to an abstract type
345+
department = new Department(); // error: cannot create an instance of an abstract class
346+
department = new AccountingDepartment(); // ok to create and assign a non-abstract subclass
347+
department.printName();
348+
department.printMeeting();
349+
department.generateReports(); // error: method doesn't exist on declared abstract type
335350
```
336351

337352
# Advanced Techniques

0 commit comments

Comments
 (0)