{"id":865,"date":"2016-02-29T06:54:12","date_gmt":"2016-02-29T06:54:12","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=865"},"modified":"2018-06-05T06:16:21","modified_gmt":"2018-06-05T06:16:21","slug":"constructor-chaining","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/constructor-chaining.html","title":{"rendered":"Constructor Chaining In Java With Example"},"content":{"rendered":"<p>Constructor Chaining is a process of calling the one constructor from another constructor with respect to current object. Whenever we want constructor to duplicate some of the behavior of an existing constructor we use constructor chaining. In the same class referring to another constructor we use <strong>this<\/strong> as the method name, followed by appropriate arguments enclosed in pair of parenthesis, if necessary. Java requires that the call to <strong>this()<\/strong> can be used in a constructor and must be the first statement in the constructor followed by any other relevant code.<\/p>\n<hr \/>\n<h4><strong>Program Example of Constructor Chaining:<\/strong><\/h4>\n<p>Let us consider a example to show use of constructor chaining.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First create a class <strong>Temp <\/strong>in which we take three constructors:<\/p>\n<pre>class Temp\r\n\r\n{\r\n\r\n  Temp()  \/\/default constructor\r\n\r\n  {\r\n\r\n    this(10);\r\n    System.out.println(\"default constructor\");\r\n\r\n  }\r\n\r\n  Temp(int x)  \/\/one parameter constructor\r\n\r\n  {\r\n\r\n    this(10,20);\r\n    System.out.println(\"value of x is\" +x);\r\n\r\n  }\r\n\r\n  Temp(int x,int y)  \/\/two parameter constructor\r\n\r\n  {\r\n\r\n    System.out.println(\"Add result is\" +(x+y));\r\n\r\n  }\r\n\r\n  public static void main(String[] args)\r\n\r\n  {\r\n\r\n    new Temp();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Add result is 30\r\nValue of x is 10\r\nDefault constructor<\/pre>\n<p><strong>Explanation of Program:<\/strong><\/p>\n<ul>\n<li>The class <strong>Temp<\/strong> contains three constructors: default constructor, one parameter constructor and two parameter constructor.<\/li>\n<li>The statement : <strong>new Temp(); <\/strong>calls a default constructor. Here it calls the one parameter constructor with the help of this(10);<\/li>\n<li>Then control goes to two parameter constructor with the help of first statement in constructor this(10,20),<\/li>\n<li>In two parameter constructor the first statement will be executed which prints the \u201cadd result is 30\u201d<\/li>\n<li>Then control goes to one parameter constructor and it executes the second statement which prints \u201cvalue of x is 10\u201d<\/li>\n<li>Then control goes to default constructor and it also execute the second statement that prints \u201cdefault constructor\u201d<\/li>\n<\/ul>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-890\" src=\"\/java\/wp-content\/uploads\/2016\/02\/Construtcor-Chaining-Example-in-JAVA.jpg\" alt=\"Construtcor Chaining Example in JAVA\" width=\"590\" height=\"344\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/Construtcor-Chaining-Example-in-JAVA.jpg 590w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/Construtcor-Chaining-Example-in-JAVA-300x175.jpg 300w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Difference Between Constructor Overloading and Constructor Chaining:<\/strong><\/h4>\n<table border=\"1\">\n<tbody>\n<tr>\n<td><strong><span style=\"color: #008000;\">Constructor Overloading<\/span><br \/>\n<\/strong><\/td>\n<td><span style=\"color: #008000;\"><strong>Constructor Chaining<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>1. Constructor overloading allows a class to have more than one constructor that have the same name as that of the class but differ only in terms of number or type of parameters.<\/td>\n<td>1. Constructor chaining is a process of calling the one constructor from another constructor with respect to current object.<\/td>\n<\/tr>\n<tr>\n<td>2. Constructor overloading is done to construct object in different ways.<\/td>\n<td>2. Constructor chaining is done to call one constructor from another constructor.<\/td>\n<\/tr>\n<tr>\n<td>3. Constructor overloading is achieved by declaring more than one constructor with same name but different parameters in a same class.<\/td>\n<td>3. Constructor chaining is achieved by this() method.<\/td>\n<\/tr>\n<tr>\n<td>4. Constructor overloading is flexible which allows us to create object in different way.<\/td>\n<td>4. Constructor chaining is useful when we have many constructors in the class and want to reuse that constructor.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h4>\u00a0<strong>Important Points to Remember About Constructor Chaining:<\/strong><\/h4>\n<ul>\n<li>Whenever you are achieving a constructor chaining using a this() method then it must be the first line in any constructor.<\/li>\n<li>Whenever we are achieving a constructor chaining using a this() method then there must be atleast one constructor which is not having a this() method as a first line.<\/li>\n<li>Constructor chaining can be achieved in any order.<\/li>\n<li>Whenever you want to provide the certain common resources to each object of a class rather than putting the code of each resource in a single constructor, make a separate constructor for each resource and then create their chain.<\/li>\n<\/ul>\n<p>Also Read: <a href=\"\/java\/constructor\">Constructor In Java With Example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Constructor Chaining is a process of calling the one constructor from another constructor with respect to current object. Whenever we want constructor to duplicate some of the behavior of an existing constructor we use constructor chaining. In the same class referring to another constructor we use this as the method name, followed by appropriate arguments &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/constructor-chaining.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Constructor Chaining In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,76],"tags":[],"class_list":["post-865","post","type-post","status-publish","format-standard","hentry","category-archieve","category-constructor"],"psp_head":"<title>Constructor Chaining In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn Constructor Chaining with example in JAVA. Also find its difference with Constructor Overloading. Constructor Chaining is a process of calling the one constructor from another constructor with respect to current object.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/constructor-chaining.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=865"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/865\/revisions"}],"predecessor-version":[{"id":1461,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/865\/revisions\/1461"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}