Super Keyword in Dart
In Dart, the super keyword is used to refer immediate parent class object. It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keyword calls that instance.
Advantages of super keyword
- It can be used to access the data members of parent class when both parent and child have member with same name.
- It is used to prevent overriding the parent method.
- It can be used to call the parameterized constructor of the parent class.
Syntax:
// To access parent class variables
super.variable_name;
// To access parent class method
super.method_name();
Showing the flow of object creation in inheritance
Example :
// Defining the parent class
class SuperGeek {
// Parent class constructor, executed when an
// instance of SuperGeek or its subclass is created
SuperGeek() {
print("You are inside the Parent constructor!!");
}
}
// Defining the child class that
// inherits from SuperGeek
class SubGeek extends SuperGeek {
// Child class constructor, executed after the parent
// constructor when an instance of SubGeek is created
SubGeek() {
print("You are inside the Child constructor!!");
}
}
void main() {
// Creating an instance of SubGeek
// This will first call the parent class
// constructor and then the child class constructor
SubGeek geek = SubGeek();
}
Output:
You are inside the Parent constructor!!
You are inside the Child constructor!!
Accessing parent class variables
Example :
// Defining the parent class
class SuperGeek {
// Declaring an instance variable
// in the parent class
String geek = "Geeks for Geeks";
}
// Defining the child class that
// inherits from SuperGeek
class SubGeek extends SuperGeek {
// Method to access and print
// the parent class variable
void printInfo() {
// 'super' keyword is used to
// access the parent class variable
print(super.geek);
}
}
void main() {
// Creating an instance of
// the child class
SubGeek geek = SubGeek();
// Calling the child class method
// to print the parent class variable
geek.printInfo();
}
Output:
Geeks for GeeksAccessing parent class methods
Example :
// Defining the parent class
class SuperGeek {
// Method in the parent class
void printInfo() {
print("Welcome to Gfg!!\nYou are inside the parent class.");
}
}
// Defining the child class that
// inherits from SuperGeek
class SubGeek extends SuperGeek {
// Method in the child class
void info() {
print("You are calling a method of the parent class.");
// Calling the parent class
// method using 'super'
super.printInfo();
}
}
void main() {
// Creating an instance of the child class
SubGeek geek = SubGeek();
// Calling the method in the child class
// that invokes the parent class method
geek.info();
}
Output:
You are calling method of parent class.
Welcome to Gfg!!
You are inside parent class.
this Keyword in Dart
While super keyword is used to call parent class, this keyword is used to call the class itself.
Advantages of this keyword
- It can be used to call instance, the current class.
- It can be used to set the values of the instance variable.
- It can be used to return the current class instance.
Using this keyword in Dart
Example :
// Defining a class
class Geek {
// Declaring an instance variable
String geek_info = "";
// Defining a parameterized constructor
Geek(String info) {
// Assigning the parameter value to the
// instance variable using 'this' keyword
this.geek_info = info;
}
// Method to print the information
void printInfo() {
print("Welcome to $geek_info");
}
}
void main() {
// Creating an instance of the Geek class
// and passing a value to the constructor
Geek geek = Geek("Geeks for Geeks");
// Calling the method to print the information
geek.printInfo();
}
Output:
Welcome to Geeks for GeeksTo know more about this keyword refer this article: Dart - this keyword.
Conclusion
- Use the keyword super to access variables, methods, and constructors from the parent class.
- Use this to refer to the instance variables and methods of the current class.
- Both keywords contribute to better structuring and understanding of class relationships in Dart.