{"id":16229,"date":"2021-11-16T16:27:48","date_gmt":"2021-11-16T10:42:48","guid":{"rendered":"https:\/\/techenum.com\/?p=16229"},"modified":"2021-11-16T16:27:50","modified_gmt":"2021-11-16T10:42:50","slug":"constructor-overloading-in-java","status":"publish","type":"post","link":"https:\/\/techenum.com\/constructor-overloading-in-java\/","title":{"rendered":"Constructor Overloading in Java | with Examples"},"content":{"rendered":"\n<p class=\"has-medium-font-size\">Let us explore what does it mean to do a constructor overloading in Java. We will explore the constructor of Java with some pretty neat code examples.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Before we dig right into the topic let us quickly revise a constructor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a constructor in Java ?<\/h2>\n\n\n\n<p class=\"has-medium-font-size\">A constructor in Java is a special method that is doesn&#8217;t behave like a normal method. It doesn&#8217;t have a <strong>return type<\/strong> defined in its declaration. And it&#8217;s name must be exactly as the name of the <strong>class<\/strong>. <\/p>\n\n\n\n<p class=\"has-medium-font-size\">If we look at a normal method. It&#8217;s main purpose is to perform some action in its own instance. But a constructor&#8217;s primary function is entirely different. Having said that, you cannot use constructor as a regular method on it&#8217;s resulting instance.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">So, then what exactly is a constructor ?<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Are you are familiar with the new keyword in Java ? If you answered yes, then you are close to understanding what a constructor is.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The constructor is invoked whenever you <strong>create an object<\/strong> of a <strong>class<\/strong>. Yes, that is whenever you do a <code>new YourClassName();<\/code>. In other words it is used to initialize the instance of an object.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Let us look at a simple code snippet.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> class <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n\n        <span class=\"hljs-comment\">\/\/ 2. this is the constructor in Java.<\/span>\n    }\n\n}\n\n<span class=\"hljs-comment\">\/\/ 1. creating a new instance of the class above<\/span>\nPerson p = <span class=\"hljs-keyword\">new<\/span> Person();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"has-medium-font-size\">In the above example when we create a <code>new Person()<\/code> the constructor method is invoked. And that sums up our constructor revisit.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Now let us move to overloading the constructor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is constructor overloading in Java ?<\/h2>\n\n\n\n<p class=\"has-medium-font-size\">Constructor overloading in Java can be defined as a way of having more than one constructor in a class. We can create many constructors with different parameters.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">This help us initialize the object in a number of different ways. Le us look at the below class for example ( <strong>read the comments<\/strong> ).<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">public <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>() <\/span>{\n\n    private int age;\n    private <span class=\"hljs-built_in\">String<\/span> firstName;\n    private <span class=\"hljs-built_in\">String<\/span> lastName;\n    private <span class=\"hljs-built_in\">String<\/span> occupation;\n\n    public Person(<span class=\"hljs-built_in\">String<\/span> firstName, <span class=\"hljs-built_in\">String<\/span> lastName, int age, <span class=\"hljs-built_in\">String<\/span> occupation) {\n\n        <span class=\"hljs-comment\">\/\/ Constructor No. 1<\/span>\n\n        <span class=\"hljs-keyword\">this<\/span>.firstName = firstName;\n        <span class=\"hljs-keyword\">this<\/span>.lastName = lastName;\n        <span class=\"hljs-keyword\">this<\/span>.age = age;\n        <span class=\"hljs-keyword\">this<\/span>.occupation = occupation;\n    }\n\n    public Person(<span class=\"hljs-built_in\">String<\/span> name, int age) {\n\n        <span class=\"hljs-comment\">\/\/ Constructor No. 2<\/span>\n\n        <span class=\"hljs-comment\">\/\/ we can overload constructor by only accepting required arguments <\/span>\n        <span class=\"hljs-comment\">\/\/ in case of this example we will accept only 2 arguments<\/span>\n        <span class=\"hljs-comment\">\/\/ and we will fill in other details here without any issue<\/span>\n\n        firstName = name;\n        <span class=\"hljs-keyword\">this<\/span>.age = age;\n\n        lastName = <span class=\"hljs-string\">\"\"<\/span>;              <span class=\"hljs-comment\">\/\/ fill in empty last name<\/span>\n        occupation = <span class=\"hljs-string\">\"Unemployed\"<\/span>;  <span class=\"hljs-comment\">\/\/ set desired occupation<\/span>\n\n    }\n\n    public Person(<span class=\"hljs-built_in\">String<\/span> name) {\n\n        <span class=\"hljs-comment\">\/\/ Constructor No. 3<\/span>\n\n        <span class=\"hljs-comment\">\/\/ we can also add another constructor that accepts only one argument<\/span>\n        <span class=\"hljs-comment\">\/\/ let us supply only the name of the person and fill in other details here.<\/span>\n\n        firstName = name;\n\n        lastName   = <span class=\"hljs-string\">\"\"<\/span>;        <span class=\"hljs-comment\">\/\/ empty<\/span>\n        occupation = <span class=\"hljs-string\">\"Student\"<\/span>; <span class=\"hljs-comment\">\/\/ everyone is studying<\/span>\n        age        = <span class=\"hljs-number\">20<\/span>;        <span class=\"hljs-comment\">\/\/ every person object created with this constructor will be 20 year old<\/span>\n    }\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"has-medium-font-size\">As we can see that in the class above we have in total 3 constructors. In other words this is known as constructor overloading.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">And we have 3 constructor overloads. Next let us understand why do we require constructor overloading in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why use constructor overloading in Java ?<\/h2>\n\n\n\n<p class=\"has-medium-font-size\">Now that we have seen how we do constructor overloading. Let us understand why do we need constructor overloading in Java.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">We use constructor overloading approach in Java to initialize the object in multiple different ways.  It adds some flexibility to the object construction.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Let&#8217;s say you need to set the values while the object is being created like in the examples explained below. Then constructor overloading is a desirable approach.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">As we can see in the code example above. We have multiple constructors initializing the object in multiple ways.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">For instance let us take an example of <code>Constructor No. 3<\/code>. It&#8217;s obvious from the constructor that the resulting object is a <code>Student<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">And <code>Constructor No. 2<\/code> is used when we want to create a person who is <code>Unemployed<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">So that was a quick explanation about constructor overloading. Keep learning and coding.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Suggested<\/strong><\/p>\n\n\n\n<p style=\"font-size:18px\"><a href=\"https:\/\/techenum.com\/java-list-data-structure-learn-real-use-case-of-list\/\">Java List Data Structure: Learn Real Use Case Of List<\/a><\/p>\n\n\n\n<p style=\"font-size:18px\"><a href=\"https:\/\/techenum.com\/arraylist-in-java\/\">ArrayList in Java: Understand With Example<\/a><\/p>\n\n\n\n<p style=\"font-size:18px\"><a href=\"https:\/\/techenum.com\/learn-to-implement-run-length-encoding-in-java\/\">Run Length Encoding in Java: Learn How To Implement<\/a><\/p>\n\n\n\n<p style=\"font-size:18px\"><a href=\"https:\/\/techenum.com\/interface-in-oop-guide-to-polymorphism-and-callbacks\/\">Interface in OOP: Guide to Polymorphism and Callbacks<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let us explore what does it mean to do a constructor overloading in Java. We will explore the constructor of Java with some pretty neat<\/p>\n","protected":false},"author":2,"featured_media":16358,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,16],"tags":[190,45],"class_list":["post-16229","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-programming","tag-constructor","tag-java"],"_links":{"self":[{"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/posts\/16229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/comments?post=16229"}],"version-history":[{"count":0,"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/posts\/16229\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/media\/16358"}],"wp:attachment":[{"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/media?parent=16229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/categories?post=16229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techenum.com\/wp-json\/wp\/v2\/tags?post=16229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}