Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Method Overriding and super | Inheritance and Abstraction
Dart OOP Essentials

bookMethod Overriding and super

Understanding how to customize inherited behavior is a key skill in Dart's object-oriented programming. Method overriding lets you redefine how a method works in a subclass, tailoring it to your needs while preserving the overall contract established by the superclass. The super keyword provides a way to access the original implementation in the superclass. This combination gives you both flexibility and structure: you can change what needs to be changed, but still rely on existing, tested behavior where appropriate.

Overriding methods with @override
expand arrow

The @override annotation is placed above a method in the subclass to signal that you are intentionally replacing a method from the superclass. Dart does not require this annotation, but it helps catch mistakes—such as misspelling a method name—by alerting you if there is nothing to override. It also makes your code clearer to other developers.

Using super to call parent methods
expand arrow

When you override a method, the superclass’s implementation is hidden. If you want to use or extend the original logic, you can call it with super.methodName(). This ensures that the base behavior is preserved and possibly enhanced, rather than discarded.

Overriding a method means providing a new implementation in a subclass for a method that is already defined in its parent class. This is crucial when a more specific behavior is needed in the subclass. In Dart, you use the @override annotation to indicate your intention to override a method. The super keyword, on the other hand, allows you to call the superclass’s version of a method from within your override. This is especially useful when you want to build on the existing logic rather than replace it entirely.

Note
Note

The @override annotation is a Dart language feature that signals to both the compiler and human readers that a method is meant to replace a method in the superclass. It improves code clarity and helps prevent errors due to typos or incorrect method signatures.

main.dart

main.dart

copy
123456789101112131415161718
class Animal { void speak() { print('The animal makes a sound.'); } } class Bird extends Animal { @override void speak() { super.speak(); print('The bird chirps.'); } } void main() { var myBird = Bird(); myBird.speak(); }

The Bird class inherits from Animal and overrides the speak() method. By calling super.speak(), Bird ensures that the general animal sound is output before adding its own specific behavior. This pattern is powerful when you want to both customize and reuse existing logic. The Bird class customizes the message by appending its own output, but does not discard the foundational behavior provided by Animal.

main.dart

main.dart

copy
123456789101112131415161718
class Car { void honk() { print('Car honks: Beep!'); } } class ElectricCar extends Car { @override void honk() { super.honk(); print('ElectricCar honks: Whirr!'); } } void main() { var tesla = ElectricCar(); tesla.honk(); }
question mark

What does the super keyword allow you to do when overriding a method in Dart?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookMethod Overriding and super

Desliza para mostrar el menú

Understanding how to customize inherited behavior is a key skill in Dart's object-oriented programming. Method overriding lets you redefine how a method works in a subclass, tailoring it to your needs while preserving the overall contract established by the superclass. The super keyword provides a way to access the original implementation in the superclass. This combination gives you both flexibility and structure: you can change what needs to be changed, but still rely on existing, tested behavior where appropriate.

Overriding methods with @override
expand arrow

The @override annotation is placed above a method in the subclass to signal that you are intentionally replacing a method from the superclass. Dart does not require this annotation, but it helps catch mistakes—such as misspelling a method name—by alerting you if there is nothing to override. It also makes your code clearer to other developers.

Using super to call parent methods
expand arrow

When you override a method, the superclass’s implementation is hidden. If you want to use or extend the original logic, you can call it with super.methodName(). This ensures that the base behavior is preserved and possibly enhanced, rather than discarded.

Overriding a method means providing a new implementation in a subclass for a method that is already defined in its parent class. This is crucial when a more specific behavior is needed in the subclass. In Dart, you use the @override annotation to indicate your intention to override a method. The super keyword, on the other hand, allows you to call the superclass’s version of a method from within your override. This is especially useful when you want to build on the existing logic rather than replace it entirely.

Note
Note

The @override annotation is a Dart language feature that signals to both the compiler and human readers that a method is meant to replace a method in the superclass. It improves code clarity and helps prevent errors due to typos or incorrect method signatures.

main.dart

main.dart

copy
123456789101112131415161718
class Animal { void speak() { print('The animal makes a sound.'); } } class Bird extends Animal { @override void speak() { super.speak(); print('The bird chirps.'); } } void main() { var myBird = Bird(); myBird.speak(); }

The Bird class inherits from Animal and overrides the speak() method. By calling super.speak(), Bird ensures that the general animal sound is output before adding its own specific behavior. This pattern is powerful when you want to both customize and reuse existing logic. The Bird class customizes the message by appending its own output, but does not discard the foundational behavior provided by Animal.

main.dart

main.dart

copy
123456789101112131415161718
class Car { void honk() { print('Car honks: Beep!'); } } class ElectricCar extends Car { @override void honk() { super.honk(); print('ElectricCar honks: Whirr!'); } } void main() { var tesla = ElectricCar(); tesla.honk(); }
question mark

What does the super keyword allow you to do when overriding a method in Dart?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
some-alt