Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Inheritance Basics | Inheritance and Class Hierarchies
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookInheritance Basics

Inheritance is a core concept in object-oriented programming, allowing you to create new classes based on existing ones. In TypeScript, inheritance is implemented using the extends keyword. When you define a class that extends another, the new class is called a derived class (or subclass), and the class it is based on is called the base class (or superclass). The derived class automatically inherits all public and protected properties and methods from the base class, which means you can reuse code and build logical hierarchies in your applications.

123456789101112131415161718192021222324
class Animal { name: string; constructor(name: string) { this.name = name; } speak(): void { console.log(`${this.name} makes a noise.`); } } class Dog extends Animal { constructor(name: string) { super(name); } speak(): void { console.log(`${this.name} barks.`); } } const myDog = new Dog("Rex"); myDog.speak(); // Output: Rex barks.
copy

By using inheritance, you can avoid duplicating code across classes that share common behavior. For example, the Dog class inherits the name property and the speak method from the Animal class, but can also define or override its own methods. This structure makes it easier to organize code, maintain large codebases, and represent real-world relationships between entities using a class hierarchy.

question mark

What does the extends keyword do in a TypeScript class declaration?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 5

bookInheritance Basics

Veeg om het menu te tonen

Inheritance is a core concept in object-oriented programming, allowing you to create new classes based on existing ones. In TypeScript, inheritance is implemented using the extends keyword. When you define a class that extends another, the new class is called a derived class (or subclass), and the class it is based on is called the base class (or superclass). The derived class automatically inherits all public and protected properties and methods from the base class, which means you can reuse code and build logical hierarchies in your applications.

123456789101112131415161718192021222324
class Animal { name: string; constructor(name: string) { this.name = name; } speak(): void { console.log(`${this.name} makes a noise.`); } } class Dog extends Animal { constructor(name: string) { super(name); } speak(): void { console.log(`${this.name} barks.`); } } const myDog = new Dog("Rex"); myDog.speak(); // Output: Rex barks.
copy

By using inheritance, you can avoid duplicating code across classes that share common behavior. For example, the Dog class inherits the name property and the speak method from the Animal class, but can also define or override its own methods. This structure makes it easier to organize code, maintain large codebases, and represent real-world relationships between entities using a class hierarchy.

question mark

What does the extends keyword do in a TypeScript class declaration?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 1
some-alt