Super Constructor in Dart
Last Updated : 02 Apr, 2025
In Dart, the subclass can inherit all the variables and methods of the parent class with the use of the extends keyword, but it can't inherit the constructor of the parent class. To do so, we make use of a super constructor in the dart. There are two ways to call a super constructor:
Implicit super
In this case, the parent class is called implicitly when there is object creation of the child class. Here, we don't make use of the super constructor, but when the child class constructor is invoked, then it calls the default parent class constructor.
Calling parent constructor taking no parameter(s)
Example:
Dart // Dart program for calling parent // constructor taking no parameter class SuperGeek { // Creating parent constructor SuperGeek(){ print("You are inside Parent constructor!!"); } } class SubGeek extends SuperGeek { // Creating child constructor SubGeek(){ print("You are inside Child constructor!!"); } } void main() { SubGeek geek = new SubGeek(); }
Output:
You are inside Parent constructor!!
You are inside Child constructor!!
Explicit super
If the parent constructor is default then we call it as followed in implicit super, but if it takes parameters, then the superclass is invoked as shown in the syntax mentioned above.
When calling explicitly, we make use of super constructor as:
Child_class_constructor() :super() {
...
}
Calling parent constructor taking parameter(s)
Example:
Dart class SuperGeek { // Creating parent constructor SuperGeek(String geek_name){ print("You are inside Parent constructor!!"); print("Welcome to $geek_name"); } } class SubGeek extends SuperGeek { // Creating child constructor and // calling parent class constructor SubGeek() : super("Geeks for Geeks"){ print("You are inside Child constructor!!"); } } void main() { SubGeek geek = new SubGeek(); }
Output:
You are inside Parent constructor!!
Welcome to Geeks for Geeks
You are inside Child constructor!!
Conclusion
In Dart, a subclass inherits all the properties and methods of its parent class by using the extends keyword. However, it does not inherit the constructors of the parent class. To address this, the superclass constructor is invoked using the super keyword. This can be done implicitly, where the parent's default constructor is automatically called, or explicitly, where parameters are passed to the parent constructor using super(). This process ensures that the parent class is properly initialized before the child class constructor is executed.
Explore
Dart Tutorial
7 min read
Basics
Data Types
Control Flow
Key Functions
Object-Oriented Programming
Dart Utilities
Dart Programs
Advance Concepts
My Profile