Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Method Overriding and super | Inheritance and Class Hierarchies
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookMethod Overriding and super

123456789101112131415161718
class Animal { makeSound() { console.log("Some generic animal sound"); } } class Dog extends Animal { makeSound() { super.makeSound(); console.log("Woof!"); } } const myDog = new Dog(); myDog.makeSound(); // Output: // Some generic animal sound // Woof!
copy

When you create a derived class in TypeScript, you can override methods from the base class to change or extend their behavior. This is called method overriding. To override a method, you simply define a method with the same name in the derived class. If you want to use the original implementation from the base class as part of your new method, you can call it using the super keyword. The super keyword refers to the parent class and allows you to access its methods or constructor.

By calling super.methodName(), you can invoke the base class version of a method before or after adding extra logic in your derived class. This is especially useful when you want to extend the functionality rather than completely replace it. Overriding methods and using super helps you build flexible and maintainable class hierarchies, where shared behavior is defined once in a base class, and specialized behavior is added in derived classes.

question mark

What is the purpose of the super keyword in a derived class?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain more about how method overriding works in TypeScript?

What happens if I don't use the super keyword in the derived class?

Can you give another example of method overriding with different classes?

Awesome!

Completion rate improved to 5

bookMethod Overriding and super

Svep för att visa menyn

123456789101112131415161718
class Animal { makeSound() { console.log("Some generic animal sound"); } } class Dog extends Animal { makeSound() { super.makeSound(); console.log("Woof!"); } } const myDog = new Dog(); myDog.makeSound(); // Output: // Some generic animal sound // Woof!
copy

When you create a derived class in TypeScript, you can override methods from the base class to change or extend their behavior. This is called method overriding. To override a method, you simply define a method with the same name in the derived class. If you want to use the original implementation from the base class as part of your new method, you can call it using the super keyword. The super keyword refers to the parent class and allows you to access its methods or constructor.

By calling super.methodName(), you can invoke the base class version of a method before or after adding extra logic in your derived class. This is especially useful when you want to extend the functionality rather than completely replace it. Overriding methods and using super helps you build flexible and maintainable class hierarchies, where shared behavior is defined once in a base class, and specialized behavior is added in derived classes.

question mark

What is the purpose of the super keyword in a derived class?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 2
some-alt