Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Abstract Classes Basics | Abstract Classes and Interfaces
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookAbstract 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.

1234567891011121314151617
abstract 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.
copy

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.

question mark

What is a key characteristic of an abstract class in TypeScript?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5

bookAbstract Classes Basics

Svep för att visa menyn

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.

1234567891011121314151617
abstract 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.
copy

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.

question mark

What is a key characteristic of an abstract class in TypeScript?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt