Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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

bookAbstract Classes Basics

Swipe to show menu

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
some-alt