Classes and Inheritance
40 minTypeScript classes support access modifiers (public, private, protected), constructors, and inheritance. Access modifiers control visibility of class members: `public` is the default and allows access from anywhere, `private` restricts access to the class itself, and `protected` allows access from the class and its subclasses. These modifiers provide encapsulation and help prevent unintended access to internal implementation details.
Classes can implement interfaces and extend other classes. When a class implements an interface, it must provide implementations for all interface members. When a class extends another class, it inherits all members and can override methods. TypeScript supports single inheritance (one parent class) but multiple interface implementation, providing flexibility in class design.
Abstract classes provide a base for other classes and can contain abstract methods. Abstract classes cannot be instantiated directly—they must be extended. Abstract methods must be implemented by subclasses, enforcing a contract while allowing flexibility in implementation. This pattern is useful for defining common behavior while leaving specific details to subclasses.
TypeScript classes support static members, which belong to the class itself rather than instances. Static methods and properties are useful for utility functions and shared data. Classes also support getters and setters for computed properties, providing a clean API for accessing and modifying data with validation or side effects.
Parameter properties allow you to declare and initialize class members in the constructor parameters. This reduces boilerplate when creating classes with many properties. For example, `constructor(public name: string)` automatically creates a public `name` property and assigns the parameter value to it.
Understanding class inheritance, method overriding, and the `super` keyword is crucial for object-oriented TypeScript development. The `super` keyword allows access to parent class members and is essential when overriding methods while still calling the parent implementation.
Key Concepts
- Access modifiers (public, private, protected) control member visibility.
- Classes can implement interfaces and extend other classes.
- Abstract classes cannot be instantiated and can contain abstract methods.
- Static members belong to the class, not instances.
- Parameter properties reduce constructor boilerplate.
Learning Objectives
Master
- Creating classes with access modifiers and constructors
- Implementing interfaces and extending classes
- Using abstract classes and abstract methods
- Understanding inheritance and method overriding
Develop
- Object-oriented design thinking
- Understanding encapsulation and inheritance
- Designing class hierarchies effectively
Tips
- Use private for internal implementation details that shouldn't be accessed externally.
- Use protected for members that subclasses need access to.
- Prefer composition over inheritance when possible for more flexible designs.
- Use abstract classes when you want to provide a base implementation with some abstract methods.
Common Pitfalls
- Overusing inheritance, creating deep hierarchies that are hard to maintain.
- Not using access modifiers, exposing internal implementation details.
- Forgetting to call super() in subclass constructors when the parent has a constructor.
- Using classes when simple objects or functions would suffice.
Summary
- Classes support access modifiers for encapsulation.
- Classes can implement interfaces and extend other classes.
- Abstract classes provide base implementations with abstract methods.
- Understanding classes is essential for object-oriented TypeScript development.
Exercise
Create a class hierarchy with inheritance and access modifiers.
abstract class Animal {
protected name: string;
constructor(name: string) {
this.name = name;
}
abstract makeSound(): string;
}
class Dog extends Animal {
makeSound(): string {
return `${this.name} says woof!`;
}
}
const myDog = new Dog("Buddy");
console.log(myDog.makeSound());
Exercise Tips
- Add more animal classes (Cat, Bird) to demonstrate polymorphism.
- Add a public method that uses the protected name property.
- Create an interface and have the class implement it.
- Add static methods to the Animal class for utility functions.