

{"id":15827,"date":"2018-05-17T04:05:06","date_gmt":"2018-05-17T04:05:06","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=15827"},"modified":"2026-05-18T10:59:32","modified_gmt":"2026-05-18T05:29:32","slug":"constructor-chaining-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/","title":{"rendered":"Constructor Chaining in Java &#8211; Changing Order &amp; Using Super () Keyword"},"content":{"rendered":"<p>In our previous tutorial, we discussed <a href=\"https:\/\/data-flair.training\/blogs\/constructor-overloading-in-java\/\" target=\"_blank\" rel=\"noopener\"><strong>Copy Constructor and Constructor Overloading in Java<\/strong> <\/a>in detail. Here, we will learn about Constructor Chaining in Java with examples. Moreover, we will check what happens if we change the order of constructors and Java constructor chaining to another class using the super() keyword.<\/p>\n<p>So, let&#8217;s start\u00a0Constructor Chaining in Java.<\/p>\n<h3>What is Constructor Chaining in Java?<\/h3>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/constructor-in-java\/\" target=\"_blank\" rel=\"noopener\">Java constructor<\/a><\/strong> chaining is a method of calling one constructor with the help of another while considering the present object.<\/p>\n<p>It can be done in 2 ways \u2013<\/p>\n<ul>\n<li><strong>Within the same class:<\/strong> It can be done using this() keyword for constructors in the same class.<\/li>\n<li><strong>From base class:<\/strong> By using the super() keyword to call a constructor from the base class.<\/li>\n<\/ul>\n<p>Constructor chaining happens through legacy. A subclass constructor&#8217;s undertaking is to call superclass&#8217;s constructor first. Thus, this guarantees the formation of a subclass protest begins with the introduction of the information from individuals in the superclass. There could be any number of classes in a legacy chain. Each constructor calls up the chain till the class at the top is reached.<\/p>\n<p>This procedure is utilized when we need to perform various tasks in a solitary constructor as opposed to writing code for each task in a solitary constructor. We make a different constructor for each undertaking and make their chain, which makes the program clearer.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/java-regular-expression\/\" target=\"_blank\" rel=\"noopener\">Do you know about Java Regular Expression (Java Regex) with Examples<\/a><\/strong><\/p>\n<div id=\"attachment_15875\" style=\"width: 530px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-In-Java1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-15875\" class=\"wp-image-15875 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-In-Java1.png\" alt=\"Constructor Chaining In Java\" width=\"520\" height=\"456\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-In-Java1.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-In-Java1-150x132.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-In-Java1-300x263.png 300w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/a><p id=\"caption-attachment-15875\" class=\"wp-caption-text\">Constructor Chaining In Java<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">class Temp\r\n{\r\nTemp()\r\n{\r\nthis(5);\r\nSystem.out.println(\"The Default constructor\");\r\n}\r\nTemp(int x)\r\n{\r\nthis(5, 15);\r\nSystem.out.println(x);\r\n}\r\nTemp(int x, int y)\r\n{\r\nSystem.out.println(x * y);\r\n}\r\npublic static void main(String args[])\r\n{\r\nnew Temp();\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><br \/>\n75<br \/>\n5<br \/>\nThe Default constructor<\/p>\n<h4>What happens if we change the order of constructors?<\/h4>\n<pre class=\"EnlighterJSRAW\">class Temp\r\n{\r\nTemp()\r\n{\r\nSystem.out.println(\"default\");\r\n}\r\nTemp(int x)\r\n{\r\nthis();\r\nSystem.out.println(x);\r\n}\r\nTemp(int x, int y)\r\n{\r\nthis(5);\r\nSystem.out.println(x * y);\r\n}\r\npublic static void main(String args[])\r\n{\r\nnew Temp(8, 10);\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><br \/>\ndefault<br \/>\n5<br \/>\n80<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/jdbc-connection\/\" target=\"_blank\" rel=\"noopener\">Do you know How to Establish JDBC Connection in Java?<\/a><\/strong><\/p>\n<h4>Constructor Chaining to other class using super() keyword<\/h4>\n<p>Calling a constructor of the parent class from the child class is called constructor chaining. Constructor chaining is implemented by using the super() keyword. Super() is used to invoke the constructor of a base class. The super must be used in the first statement of the derived class during the calling of a constructor.<\/p>\n<p>Super also allows passing a parameter so that the variable gets initialised and can be used by the method of the child class.<\/p>\n<pre class=\"EnlighterJSRAW\">class Base\r\n{\r\nString name;\r\nBase()\r\n{\r\nthis(\"\");\r\nSystem.out.println(\"No-argument constructor of\" +\r\n\" base class\");\r\n}\r\nBase(String name)\r\n{\r\nthis.name = name;\r\nSystem.out.println(\"Calling parameterized constructor\"\r\n+ \" of base\");\r\n}\r\n}\r\nclass Derived extends Base\r\n{\r\nDerived()\r\n{\r\nSystem.out.println(\"No-argument constructor \" +\r\n\"of derived\");\r\n}\r\nDerived(String name)\r\n{\r\nsuper(name);\r\nSystem.out.println(\"Calling parameterized \" +\r\n\"constructor of derived\");\r\n}\r\npublic static void main(String args[])\r\n{\r\nDerived obj = new Derived(\"test\");\r\n}\r\n}<\/pre>\n<p><strong>Output-<\/strong><br \/>\nCalling parameterized constructor of a base<br \/>\nCalling parameterized constructor of derived<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/difference-between-abstract-class-and-interface-in-java\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s explore the difference between an abstract class and an interface in Java<\/a><\/strong><\/p>\n<h3>Error Handling in Constructor Chaining<\/h3>\n<p>Constructor chaining can be used to implement robust error-handling mechanisms. By throwing exceptions from constructors and handling them in higher-level constructors, you can ensure that objects are always created in a valid state. For instance, a constructor might throw an IllegalArgumentException if an invalid parameter is passed. However, A higher-level constructor can catch this exception, log the error, and provide a default value for the parameter.<\/p>\n<h3>Benefits of Constructor Chaining<\/h3>\n<p><strong>1. Improved Code Reusability:<\/strong> Constructor chaining allows you to reuse common initialization logic across different constructors, reducing code duplication.<\/p>\n<p><strong>2. Enhanced Readability:<\/strong> By delegating initialization steps to smaller constructors, complex object creation becomes easier to understand, following a clear step-by-step process.<\/p>\n<p><strong>3. Better Maintainability:<\/strong> Breaking down initialization into smaller chunks makes code easier to modify and maintain in the future.<\/p>\n<p>So, this was all about\u00a0Constructor Chaining in Java Tutorial. Hope you like our explanation<b><\/b>.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, we learned about Constructor Chaining in the\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/best-java-books\/\">Java Programming Language<\/a><\/strong>. In addition, we checked two conditions: what happens if we change the order of constructors and Java constructor chaining to another class using the super() keyword. We hope that through this document, we will solve all your queries. Share your feedback in the comment section.<\/p>\n<p>Related Topics-\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/wrapper-class-in-java\/\" target=\"_blank\" rel=\"noopener\">Wrapper Class in Java\u00a0<\/a><\/strong><\/p>\n<p><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Constructor_(object-oriented_programming)\" target=\"_blank\" rel=\"noopener\">For reference<\/a><\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1948,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Constructor_(object-oriented_programming)&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;new&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous tutorial, we discussed Copy Constructor and Constructor Overloading in Java in detail. Here, we will learn about Constructor Chaining in Java with examples. Moreover, we will check what happens if we&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":15853,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[2928,4258,7442],"class_list":["post-15827","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-constructor-chaining-in-java","tag-example-of-constructor-chaining-in-java","tag-java-constructor-chaining"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constructor Chaining in Java - Changing Order &amp; Using Super () Keyword - DataFlair<\/title>\n<meta name=\"description\" content=\"Constructor Chaining is a method of calling one constructor with the help of another. Let&#039;s understand constructor chaining with an example.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructor Chaining in Java - Changing Order &amp; Using Super () Keyword - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Constructor Chaining is a method of calling one constructor with the help of another. Let&#039;s understand constructor chaining with an example.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-17T04:05:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-18T05:29:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-in-Java-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constructor Chaining in Java - Changing Order &amp; Using Super () Keyword - DataFlair","description":"Constructor Chaining is a method of calling one constructor with the help of another. Let's understand constructor chaining with an example.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Constructor Chaining in Java - Changing Order &amp; Using Super () Keyword - DataFlair","og_description":"Constructor Chaining is a method of calling one constructor with the help of another. Let's understand constructor chaining with an example.","og_url":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-17T04:05:06+00:00","article_modified_time":"2026-05-18T05:29:32+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-in-Java-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Constructor Chaining in Java &#8211; Changing Order &amp; Using Super () Keyword","datePublished":"2018-05-17T04:05:06+00:00","dateModified":"2026-05-18T05:29:32+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/"},"wordCount":611,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-in-Java-01.jpg","keywords":["Constructor Chaining in Java","Example of Constructor Chaining in Java","Java Constructor Chaining"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/","url":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/","name":"Constructor Chaining in Java - Changing Order &amp; Using Super () Keyword - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-in-Java-01.jpg","datePublished":"2018-05-17T04:05:06+00:00","dateModified":"2026-05-18T05:29:32+00:00","description":"Constructor Chaining is a method of calling one constructor with the help of another. Let's understand constructor chaining with an example.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-in-Java-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Constructor-Chaining-in-Java-01.jpg","width":1200,"height":628,"caption":"Constructor Chaining in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/constructor-chaining-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Constructor Chaining in Java &#8211; Changing Order &amp; Using Super () Keyword"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/15827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=15827"}],"version-history":[{"count":10,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/15827\/revisions"}],"predecessor-version":[{"id":148318,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/15827\/revisions\/148318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/15853"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=15827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=15827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=15827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}