Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Inheritance Basics | Inheritance and Class Hierarchies
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how method overriding works in this example?

What is the difference between public and protected properties in inheritance?

Can you show how to add another subclass, like Cat, that also extends Animal?

Awesome!

Completion rate improved to 5

bookInheritance Basics

Scorri per mostrare il menu

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 1
some-alt