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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 5

bookMethod Overriding and super

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 2
some-alt