Classes
- Extends: Inherits all properties and methods from parent class
Extends
- Extends another class
- Inherits the methods from a parent class
class Car extends Vehicle {}
class NumbersCollection extends Sorter {}
Implements
- Implements a interface
- Assure that the class obeys the interface
- Check if class satisfies all the requisites in an interface
class User implements Mappable {}
class NumbersCollection implements Sortable {}
Abstract classes
- Classes that cannot be instantiated directly!
- Only used as a parent class
- Methods can refer to methods that don't exist yet
- Child classes must 'promise' to implement theses methods
abstract class Sorter {
abstract compare(leftIndex: number, rightIndex: number): boolean;
abstract swap(leftIndex: number, rightIndex: number): void;
abstract length: number;
}
- Abstract keyword mark methods that will be defined in the child class
super()
- super() must be called inside of the constructor of a child class
- super() is a reference to the constructor of the parent class
- super() don't need to be called if the child does not have a constructor defined
export class CharactersCollection extends Sorter {
constructor(public data: string) {
super();
}
}
Static methods
- Invoke a method without instantiating the class
class Food {
// Static Property
static description = 'This class represents foods!';
// Static method
static printDescription() {
console.log(this.description);
}
}