Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Class Inheritance with extends | Inheritance and Encapsulation
JavaScript Classes and OOP Foundations

bookClass Inheritance with extends

Understanding how to build relationships between classes is a key part of mastering object-oriented programming in JavaScript. The extends keyword is central to this process. When you use extends, you create a new class (a subclass or child class) that inherits the properties and methods of another class (the parent or base class). This allows you to reuse and organize code efficiently, letting subclasses specialize or expand on the behavior defined in their parent classes.

For example, you might have a general Animal class that describes features common to all animals. Then, you can create a Dog class that extends Animal, automatically gaining all the abilities and properties of Animal while also adding features unique to dogs.

123456789101112131415161718
class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a sound.`); } } class Dog extends Animal { bark() { console.log(`${this.name} barks.`); } } const myDog = new Dog("Rex"); myDog.speak(); // Rex makes a sound. myDog.bark(); // Rex barks.
copy
question mark

Which statement best describes the effect of using the extends keyword in a JavaScript class definition?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how inheritance works in this example?

What happens if I add a method with the same name in both the parent and child class?

Can you show how to add more subclasses that extend Animal?

Awesome!

Completion rate improved to 6.25

bookClass Inheritance with extends

Swipe um das Menü anzuzeigen

Understanding how to build relationships between classes is a key part of mastering object-oriented programming in JavaScript. The extends keyword is central to this process. When you use extends, you create a new class (a subclass or child class) that inherits the properties and methods of another class (the parent or base class). This allows you to reuse and organize code efficiently, letting subclasses specialize or expand on the behavior defined in their parent classes.

For example, you might have a general Animal class that describes features common to all animals. Then, you can create a Dog class that extends Animal, automatically gaining all the abilities and properties of Animal while also adding features unique to dogs.

123456789101112131415161718
class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a sound.`); } } class Dog extends Animal { bark() { console.log(`${this.name} barks.`); } } const myDog = new Dog("Rex"); myDog.speak(); // Rex makes a sound. myDog.bark(); // Rex barks.
copy
question mark

Which statement best describes the effect of using the extends keyword in a JavaScript class definition?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt