In Dart, the super keyword is used to refer to the parent class (superclass) of the current class. It allows a subclass to access methods, properties, or constructors of its superclass, which is helpful when you want to extend or modify existing behavior without completely overriding it.
class Vehicle {
void start() {
print('Vehicle started');
}
}
class Car extends Vehicle {
@override
void start() {
super.start(); // calls Vehicle's start()
print('Car is ready to go'); // additional behavior
}
}
void main() {
var myCar = Car();
myCar.start();
}
Here, super.start() calls the start() method of Vehicle before adding extra behavior specific to Car.
I’ve applied super in Flutter projects when creating custom widgets that extend base widgets. For instance, I overrode a method in a custom button widget but still wanted to call the base class method to maintain the original functionality, and super made that possible.
A challenge I faced was understanding when to use super inside constructors versus methods. In constructors, super() is used to call the parent’s constructor, and it must be called before initializing the subclass fields. A limitation is that Dart only allows single inheritance, so super only refers to one parent class. For combining behaviors from multiple sources, mixins or composition are alternatives.
