Abstract Classes Basics
You will often encounter situations where you want to define a class that serves as a blueprint for other classes, but should never be instantiated directly. This is where abstract classes come into play in TypeScript. An abstract class lets you specify certain methods or properties that must be implemented by subclasses, while also allowing you to provide shared functionality. You use an abstract class when you want to define a common contract for other classes, but you do not want the base class itself to create objects.
1234567891011121314151617abstract class Animal { abstract makeSound(): void; // Abstract method move(): void { // Concrete method console.log("The animal moves."); } } class Dog extends Animal { makeSound(): void { console.log("Woof!"); } } const myDog = new Dog(); myDog.makeSound(); // Output: Woof! myDog.move(); // Output: The animal moves.
The Animal class above demonstrates two important features of abstract classes. First, it declares an abstract method called makeSound(). This method has no implementation in the base class and must be provided by any subclass. Second, it includes a concrete method called move(), which has a regular implementation that all subclasses inherit. Abstract classes cannot be instantiated directly—you cannot write new Animal()—because they are incomplete by design. Only subclasses that implement all abstract methods can be instantiated.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you explain more about when to use abstract classes versus interfaces in TypeScript?
What happens if a subclass does not implement all abstract methods?
Can you show an example with multiple subclasses inheriting from the abstract class?
Awesome!
Completion rate improved to 5
Abstract Classes Basics
Pyyhkäise näyttääksesi valikon
You will often encounter situations where you want to define a class that serves as a blueprint for other classes, but should never be instantiated directly. This is where abstract classes come into play in TypeScript. An abstract class lets you specify certain methods or properties that must be implemented by subclasses, while also allowing you to provide shared functionality. You use an abstract class when you want to define a common contract for other classes, but you do not want the base class itself to create objects.
1234567891011121314151617abstract class Animal { abstract makeSound(): void; // Abstract method move(): void { // Concrete method console.log("The animal moves."); } } class Dog extends Animal { makeSound(): void { console.log("Woof!"); } } const myDog = new Dog(); myDog.makeSound(); // Output: Woof! myDog.move(); // Output: The animal moves.
The Animal class above demonstrates two important features of abstract classes. First, it declares an abstract method called makeSound(). This method has no implementation in the base class and must be provided by any subclass. Second, it includes a concrete method called move(), which has a regular implementation that all subclasses inherit. Abstract classes cannot be instantiated directly—you cannot write new Animal()—because they are incomplete by design. Only subclasses that implement all abstract methods can be instantiated.
Kiitos palautteestasi!