{"id":3914,"date":"2022-05-31T07:04:15","date_gmt":"2022-05-31T07:04:15","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3914"},"modified":"2022-05-31T07:04:15","modified_gmt":"2022-05-31T07:04:15","slug":"dart-super-constructor","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/31\/dart-super-constructor\/","title":{"rendered":"Dart Super Constructor"},"content":{"rendered":"\n<p>The child class can inherit all properties (methods, variables) and behavior of parent expect parent class constructor.&amp; The superclass constructor can be invoke in sub class by using the&nbsp;<strong>super()<\/strong>&nbsp;constructor. We can access both non-parameterized and parameterized constructor of superclass. Accessing the constructor of superclass is slightly different in the Dart. The syntax is given below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-super-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-constructor#\"><\/a><\/p>\n\n\n\n<ol><li>SubClassConstructor():<strong>super<\/strong>()&nbsp;{&nbsp;&nbsp;<\/li><li class=\"\">}&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Implicit super<\/h2>\n\n\n\n<p>As we know that the constructor is automatically called when we instantiate a class. When we create the object of sub class, it invokes the constructor of sub class which implicitly invokes the parent class&#8217;s default(non-parameterized) constructor. We can use&nbsp;<strong>super()<\/strong>&nbsp;constructor in our subclass to invoke superclass constructor. Let&#8217;s understand the following example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-super-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-super-constructor#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Parent class  \nclass Superclass {  \n          Superclass(){  \n               print(\"This is a superclass constructor\");  \n  \n                }  \n}  \nclass Subclass extends Superclass  \n{  \n          Subclass(){  \n                print(\"This is a subclass constructor\");  \n           }  \n           display(){  \n               print(\"Welcome to javapoint\");  \n}  \n}  \nvoid main(){  \n          print(\"Dart Implicit Superclass constructor call\");  \n          \/\/ We create a object of sub class which will invoke subclass constructor.  \n          \/\/ as well as parent class constructor.   \n          Subclass s = new Subclass();  \n          \/\/ Calling sub class method  \n          s.display();  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dart Implicit Superclass constructor example\nThis is a superclass constructor\nThis is a subclass constructor\nWelcome to javapoint\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Explicit super<\/h2>\n\n\n\n<p>If the superclass constructor consists of parameters then we require to call super() constructor with argument in to invoke superclass constructor in subclass explicitly. Let&#8217;s understand the following example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example &#8211;<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Parent class  \nclass Superclass {  \n          Superclass(String msg){  \n               print(\"This is a superclass constructor\");  \n                print(msg);  \n  \n                }  \n}  \nclass Subclass extends Superclass  \n{  \n          Subclass():super(\"We are calling superclass constructor explicitly \"){  \n                print(\"This is a subclass constructor\");  \n                 \n           }  \n           display(){  \n               print(\"Welcome to javatpoint\");  \n}  \n}  \nvoid main(){  \n          print(\"Dart Implicit Superclass constructor example\");  \n          \/\/ We create an object of sub class which will invoke subclass constructor.  \n          \/\/ as well as parent class constructor.   \n          Subclass s = new Subclass();  \n          \/\/ Calling sub class method  \n          s.display();  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dart explicit Superclass constructor example\nThis is a parameterized superclass constructor\nWe are calling superclass constructor explicitly\nThis is a subclass constructor\nWelcome to javatpoint<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The child class can inherit all properties (methods, variables) and behavior of parent expect parent class constructor.&amp; The superclass constructor can be invoke in sub class by using the&nbsp;super()&nbsp;constructor. We can access both non-parameterized and parameterized constructor of superclass. Accessing the constructor of superclass is slightly different in the Dart. The syntax is given below. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[340],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3914"}],"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=3914"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3914\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}