{"id":3910,"date":"2022-05-31T06:58:14","date_gmt":"2022-05-31T06:58:14","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3910"},"modified":"2022-05-31T06:58:14","modified_gmt":"2022-05-31T06:58:14","slug":"dart-super-keyword","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/31\/dart-super-keyword\/","title":{"rendered":"Dart super Keyword"},"content":{"rendered":"\n<p>The 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&#8217;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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Usage of static Keyword<\/h2>\n\n\n\n<ul><li>When parent class and child class have members with the same name, then super keyword can be accessed data members of parent class in child class.<\/li><li>It is used to access the parent class constructor in the child class.<\/li><li>Using the super keyword, we can access the superclass method that is overridden by the subclass.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using a super keyword with variables<\/h2>\n\n\n\n<p>This situation arises when the child class has the same name variables as superclass variables. So there is a chance of ambiguity for\u00a0Dart\u00a0compiler. Then super keyword can access the superclass variables in its child class. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><\/p>\n\n\n\n<ol><li>Super.varName&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>We can more understand by the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Super class Car   \nclass Car  \n{   \n    int speed = 180;   \n}   \n    \n\/\/ sub class Bike extending Car   \nclass Bike extends Car   \n{   \n    int speed = 110;   \n    \n    void display()   \n    {   \n        \/\/print varible of the base class (Bike)  \n        print(\"The speed of car: ${super.speed}\");  \n    }   \n}   \nvoid main() {  \n\/\/ Creating object of sub class  \nBike b = new Bike();  \nb.display();  \n}    <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The speed of car: 180\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above code, we defined the superclass as&nbsp;<strong>Car,<\/strong>&nbsp;which has a speed variable, and then it inherited by the subclass Bike.<\/p>\n\n\n\n<p>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&nbsp;<strong>b<\/strong>&nbsp;and called a display method that printed the value of superclass variable.<\/p>\n\n\n\n<p>If we use the print(speed) instead of print(super.speed), it will print the value the subclass variable.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the super keyword with parent class method<\/h2>\n\n\n\n<p>As 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.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><\/p>\n\n\n\n<ol><li><strong>super<\/strong>.methodName;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the following example<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Base class Super   \nclass Super   \n{   \n    void display()   \n    {   \n        print(\"This is the super class method\");   \n    }   \n}   \n  \n\/\/ Child class inherits Super  \nclass Child extends Super   \n{   \n    void display()   \n    {   \n        print(\"This is the child class\");   \n    }   \n  \n    \/\/ Note that message() is only in Student class   \n    void message()   \n    {   \n        \/\/ will invoke or call current class display() method   \n        display();   \n  \n        \/\/ will invoke or call parent class displa() method   \n        super.display();   \n    }   \n}   \n  \nvoid main() {  \n \/\/ Creating object of sub class  \nChild c = new Child();   \n\/\/ calling display() of Student   \nc.message();   \n    }   <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is the child class method\nThis is the super class method\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above code, we created the function with the same name in both parent class and child class. The&nbsp;<strong>display()<\/strong>&nbsp;method is present in both parent class and child class that means It is a situation of method overriding.<\/p>\n\n\n\n<p>So we created a&nbsp;<strong>message()<\/strong>&nbsp;method in the child class, inside it, we called parent class method using the&nbsp;<strong>super keyword,<\/strong>&nbsp;and then created the object of the child class. Using the object, we called the&nbsp;<strong>message()<\/strong>&nbsp;method that printed both&nbsp;<strong>display()<\/strong>&nbsp;methods statements to the screen.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; The super can be used only if the subclass method overrides the superclass method. If it doesn&#8217;t then we don&#8217;t require using super keyword.<\/h4>\n\n\n\n<h2 class=\"wp-block-heading\">Using super keyword with constructor<\/h2>\n\n\n\n<p>We 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.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-keyword#\"><\/a><\/p>\n\n\n\n<ol><li>:<strong>super<\/strong>();&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Base class called Parent  \nclass Parent  \n{   \n    Parent()   \n    {   \n        print(\"This is the super class constructor\");   \n    }   \n}   \n  \n\/\/ Child class Super  \nclass Child extends Parent   \n{              \n    Child():super()   \/\/ Calling super class constructor  \n    {                   \n        print(\"This is the sub class constructor\");   \n    }   \n}  \n  \nvoid main() {  \n \/\/ Creating object of sub class  \nChild c = new Child();   \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is the super class constructor\nThis is the sub class constructor\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><\/p>\n\n\n\n<p>The syntax of Dart language is too close to\u00a0C# 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The 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&#8217;s main objective is to remove the confusion between parent class and subclass with the same method name. It is also used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3910"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3910"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3910\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}