{"id":3904,"date":"2022-05-29T18:54:46","date_gmt":"2022-05-29T18:54:46","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3904"},"modified":"2022-05-29T18:54:46","modified_gmt":"2022-05-29T18:54:46","slug":"what-is-constructor","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/29\/what-is-constructor\/","title":{"rendered":"What is constructor?"},"content":{"rendered":"\n<p>A constructor is a different type of function which is created with same name as its class name. The constructor is used to initialize an object when it is created. When we create the object of class, then constructor is automatically called. It is quite similar to class function but it has no explicit return type. The generative constructor is the most general form of the constructor, which is used to create a new instance of a class.<\/p>\n\n\n\n<p>It is option to declare within the class. All class have own constructor but if we don&#8217;t declare or forget then\u00a0Dart\u00a0compiler will create default constructor automatically by passing the default value to the member variable. If we declare own constructor, then default constructor will be ignored.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p>Suppose we have a class name&nbsp;<strong>Student<\/strong>&nbsp;and we will create an object of it as follow.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<ol><li>Student&nbsp;std&nbsp;=&nbsp;<strong>new<\/strong>&nbsp;Student()&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>It invoked the default constructor of the&nbsp;<strong>Student<\/strong>&nbsp;class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Constructor in Dart<\/h2>\n\n\n\n<p>As we have mentioned, a constructor has the same name as its class name and it doesn&#8217;t return any value. Suppose if we have class Student then the constructor name should be also&nbsp;<strong>Student<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<ol><li><strong>class<\/strong>&nbsp;ClassName&nbsp;{&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ClassName()&nbsp;{&nbsp;&nbsp;<\/li><li>}&nbsp;&nbsp;<\/li><li class=\"\">}&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>We must remember the following two rules while creating a constructor.<\/p>\n\n\n\n<ul><li>The constructor name should be same as the class name.<\/li><li>The constructor doesn&#8217;t have explicit return type.<\/li><\/ul>\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<p><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n      \/\/ Creating an object   \n      Student std = new Student(\"Jones\",26);  \n}  \nclass Student{  \n     \/\/ Declaring a construstor   \n     Student(String str, int age){  \n          print(\"The name is: ${str}\");  \n          print(\"The age is: ${age}\");  \n  \n             }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The name is: Jones\nThe age is: 26\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above example, we created a constructor function&nbsp;<strong>Student()<\/strong>&nbsp;which is same as the class name. We passed two passed parameter in the constructor and when we instantiated an object of Student class and passed value it automatically called the constructor then printed the result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Constructors<\/h2>\n\n\n\n<p>There are three types of constructors in Dart as given below.<\/p>\n\n\n\n<ul><li>Default Constructor or no-arg Constructor<\/li><li>Parameter Constructor<\/li><li>Named Constructor<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Default Constructor or no-arg Constructor<\/h2>\n\n\n\n<p>A Constructor which has no parameter is called default constructor or no-arg constructor. It is automatically created (with no argument) by Dart compiler if we don&#8217;t declare in the class. The Dart compiler ignores the default constructor if we create a constructor with argument or no argument. 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-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<ol><li><strong>class<\/strong>&nbsp;ClassName&nbsp;{&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;&nbsp;ClassName()&nbsp;{&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;\/\/&nbsp;constructor&nbsp;body&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/li><li>}&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>void main() {  \n      \/\/ Call constructor automatically when we creates an object   \n      Student std = new Student();  \n}  \n  \nclass Student{  \n     \/\/ Declaring a construstor   \n     Student(){  \n          print(\"The example of the default constructor\");  \n             }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The example of the default constructor\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Parameterized Constructor<\/h2>\n\n\n\n<p>We can also pass the parameters to a constructor that type of constructor is called parameterized constructor. It is used to initialize instance variables. Sometimes, we need a constructor which accepts single or multiple parameters. The parameterized constructors are mainly used to initialize instance variable with own values. 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-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<ol><li><strong>class<\/strong>&nbsp;ClassName&nbsp;{&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;&nbsp;ClassName(parameter_list)&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;\/\/&nbsp;constructor&nbsp;body&nbsp;&nbsp;<\/li><li class=\"\">}&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><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<ol><li><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n      \/\/ Creating an object   \n      Student std = new Student(\"Jones\",26);  \n}  \nclass Student{  \n     \/\/ Declaring a parameterized constructor   \n     Student(String str, int age){  \n          print(\"The name is: ${str}\");  \n          print(\"The age is: ${age}\");  \n  \n             }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The name is: Jones\nThe age is: 26\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above example, we declared a parameterized constructor which has two parameter name and age. We created an object of the Student class and passed the appropriate value to the constructor. It printed the name and age as an output to the screen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Named Constructors<\/h2>\n\n\n\n<p>The named constructors are used to declare the multiple constructors in single 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-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<ol><li>className.constructor_name(param_list)&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<p><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-constructor#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n      \/\/ Creating an object   \n      Student std1 = new Student();  \/\/ object for Default constructor  \n      Student std2 = new Student.namedConst(\"Computer Science\");  \/\/ object for parameterized constructor  \n}  \n  \nclass Student{  \n     \/\/ Declaring a construstor   \n     Student(){  \n          print(\"The example of the named constructor\");  \n             }  \n      \/\/ Second constructor  \n     Student.namedConst(String branch){  \n          print(\"The branch is: ${branch}\");  \n  \n           }  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The example of the named constructor\nThe branch is: Computer Science<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A constructor is a different type of function which is created with same name as its class name. The constructor is used to initialize an object when it is created. When we create the object of class, then constructor is automatically called. It is quite similar to class function but it has no explicit return [&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\/3904"}],"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=3904"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3904\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}