Dart – Super and This keyword

Super Keyword in Dart:

In Dart, 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 use to access the data members of parent class when both parent and child have member with same name.
  • It is use to prevent overriding the parent method.
  • It can be used to call parameterized constructor of parent class.

Syntax:

// To access parent class variables
super.variable_name;

// To access parent class method
super.method_name();

Example #1: Showing the flow of object creation in inheritance.

filter_none

edit
close

play_arrow

link
brightness_4
code

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();
}

chevron_right


Output:



You are inside Parent constructor!!
You are inside Child constructor!!

 

Example #2: Accessing parent class variables

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating Parent class
class SuperGeek {
    String geek = "Geeks for Geeks";
}
  
// Creating child class
class SubGeek extends SuperGeek {
  
    // Accessing parent class variable
    void printInfo()
    {
        print(super.geek);
    }
}
  
void main()
{
    // Creating child class object
    SubGeek geek = new SubGeek();
  
    // Calling child class method
    geek.printInfo();
}

chevron_right


Output:

Geeks for Geeks

 

Example #3: Accessing parent class methods

filter_none

edit
close

play_arrow

link
brightness_4
code

class SuperGeek {
  
    // Creating a method in Parent class
    void printInfo()
    {
        print("Welcome to Gfg!!\nYou are inside parent class.");
    }
}
  
class SubGeek extends SuperGeek {
  
    void info()
    {
        print("You are calling method of parent class.");
  
        // Calling parent class method
        super.printInfo();
    }
}
  
void main()
{
    SubGeek geek = new SubGeek();
    geek.info();
}

chevron_right


Output:

You are calling method of parent class.
Welcome to Gfg!!
You are inside parent class.

This Keyword in Dart:

While super keyword is use to call parent class, this keyword is use to call the class itself.

Advantages of this keyword:

  • It can be use to call instance variable of current class.
  • It can be use to set the values of the instance variable.
  • It can be use to return the current class instance.

Using this keyword in Dart –

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating class
class Geek {
  
    // Creating an instance variable
    String geek_info;
  
    // Creating a parameterized constructor
    Geek(String info)
    {
        // Calling instance variable using this keyword.
        this.geek_info = info;
    }
    void printInfo()
    {
        print("Welcome to $geek_info");
    }
}
  
void main()
{
    Geek geek = new Geek("Geeks for Geeks");
    // Calling method
    geek.printInfo();
}

chevron_right


Output:

Welcome to Geeks for Geeks



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.