Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 5

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
some-alt