Super in Dart

Introduction

In this section, you will learn about Super in Dart programming language with the help of examples. Before learning about Super in Dart, you should have a basic understanding of the constructor and inheritance in Dart.

What Is Super In Dart?

Super is used to refer to the parent class. It is used to call the parent class’s properties and methods.

Example 1: Super In Dart

In this example below, the show() method of the MacBook class calls the show() method of the parent class using the super keyword.

class Laptop {
  // Method
    void show() {
        print("Laptop show method");
    }
}

class MacBook extends Laptop {
    void show() {
        super.show(); // Calling the show method of the parent class
        print("MacBook show method");
    }
}

void main() {
  // Creating an object of the MacBook class
  MacBook macbook = MacBook();
  macbook.show();
}

Show Output
Run Online

Example 2: Accessing Super Properties In Dart

In this example below, the display() method of the Tesla class calls the noOfSeats property of the parent class using the super keyword.

class Car {
  int noOfSeats = 4;
}

class Tesla extends Car {
  int noOfSeats = 6;

  void display() {
    print("No of seats in Tesla: $noOfSeats");
    print("No of seats in Car: ${super.noOfSeats}");
  }
}

void main() {
  var tesla = Tesla();
  tesla.display();
}

Show Output
Run Online

Example 3: Super With Constructor In Dart

In this example below, the Manager class constructor calls the Employee class constructor using the super keyword.

class Employee {
  // Constructor
  Employee(String name, double salary) {
    print("Employee constructor");
    print("Name: $name");
    print("Salary: $salary");
  }
}

class Manager extends Employee {
  // Constructor
  Manager(String name, double salary) : super(name, salary) {
    print("Manager constructor");
  }
}

void main() {
  Manager manager = Manager("John", 25000.0);
}

Show Output
Run Online

Example 4: Super With Named Constructor In Dart

In this example below, the Manager class named constructor calls the Employee class named constructor using the super keyword.

class Employee {
  // Named constructor
  Employee.manager() {
    print("Employee named constructor");
  }
}

class Manager extends Employee {
  // Named constructor
  Manager.manager() : super.manager() {
    print("Manager named constructor");
  }
}

void main() {
  Manager manager = Manager.manager();
}

Show Output
Run Online

Example 5: Super With Multilevel Inheritance In Dart

In this example below, the MacBookPro class method display calls the display method of the parent class MacBook using the super keyword. The MacBook class method display calls the display method of the parent class Laptop using the super keyword.

class Laptop {
  // Method
  void display() {
    print("Laptop display");
  }
}

class MacBook extends Laptop {
  // Method
  void display() {
    print("MacBook display");
    super.display();
  }
}

class MacBookPro extends MacBook {
  // Method
  void display() {
    print("MacBookPro display");
    super.display();
  }
}

void main() {
  var macbookpro = MacBookPro();
  macbookpro.display();
}

Show Output
Run Online

Key Points To Remember

  • The super keyword is used to access the parent class members.
  • The super keyword is used to call the method of the parent class.

Video

Watch our video on super in Dart.