Dart super KeywordThe super keyword is used to denote the instant parent class object of the current child class. It is used to invoke superclass methods, superclass constructor in its child class. The super keyword's main objective is to remove the confusion between parent class and subclass with the same method name. It is also used to refer to the superclass properties and methods. Usage of static Keyword
Using a super keyword with variablesThis situation arises when the child class has the same name variables as superclass variables. So there is a chance of ambiguity for Dart compiler. Then super keyword can access the superclass variables in its child class. The syntax is given below. Syntax: We can more understand by the following example. Example - Output The speed of car: 180 Explanation: In the above code, we defined the superclass as Car, which has a speed variable, and then it inherited by the subclass Bike. The sub class also has variable speed, so we used the super keyword to access the parent class variable. We created the object of child class b and called a display method that printed the value of superclass variable. If we use the print(speed) instead of print(super.speed), it will print the value the subclass variable. You can copy the above code, paste it into your dartpad or notepad, and print the value without using the super keyword. You can see the difference between both results. Using the super keyword with parent class methodAs discussed earlier, the super keyword is used to access the parent class method in child class. If the child class and parent class consist of the same name, then we can use the super keyword to use the parent class method in child class. The syntax is given below. Syntax: Let's understand the following example Example - Output This is the child class method This is the super class method Explanation - In the above code, we created the function with the same name in both parent class and child class. The display() method is present in both parent class and child class that means It is a situation of method overriding. So we created a message() method in the child class, inside it, we called parent class method using the super keyword, and then created the object of the child class. Using the object, we called the message() method that printed both display() methods statements to the screen. Note - The super can be used only if the subclass method overrides the superclass method. If it doesn't then we don't require using super keyword.Using super keyword with constructorWe can also use the super keyword to access the parent class constructor. The super keyword can call both parameterized and non-parameterized constructors depending on the situation. The syntax is given below. Syntax: Let's understand the following example. Example - Output This is the super class constructor This is the sub class constructor Explanation The syntax of Dart language is too close to C# language. We called the parent class constructor using the super keyword, which is separated by : (colon). The delegate a superclass constructor, we should put the superclass as an initializer. Next TopicDart Inheritance |