{"id":3832,"date":"2017-07-31T07:31:37","date_gmt":"2017-07-31T07:31:37","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3832"},"modified":"2024-09-04T07:21:41","modified_gmt":"2024-09-04T07:21:41","slug":"how-to-overload-a-constructor-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-overload-a-constructor-in-java\/","title":{"rendered":"How to overload a Constructor in Java"},"content":{"rendered":"<h2>How to overload a Constructor in Java<\/h2>\n<p>In this tutorial, we will learn to overload constructor methods in Java with an example. We would take the <strong>Dog<\/strong> class as an example. We will create two constructors for the class. The default <strong>no-arg constructor<\/strong> and an <strong>overloaded constructor<\/strong> that accepts the object arguments in the parameters.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/default constructor\r\n  public Dog() {\r\n    this.name = \"JustBorn\";\r\n    this.weight = 0.1;\r\n    this.height = 0.1;\r\n  }\r\n\r\n  \/\/ overloaded constructor\r\n  public Dog(String name, double weight, double height) {\r\n    this.name = name;\r\n    this.weight = weight;\r\n    this.height = height;\r\n  }<\/pre>\n<p>&nbsp;<\/p>\n<p>The default constructor always creates the same object with the specified object properties in the default constructor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3837\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Constructors-Java.png\" alt=\"Java Constructors overload\" width=\"1729\" height=\"881\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Constructors-Java.png 1729w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Constructors-Java-300x153.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Constructors-Java-1024x522.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Constructors-Java-768x391.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Constructors-Java-1536x783.png 1536w\" sizes=\"auto, (max-width: 1729px) 100vw, 1729px\" \/><\/p>\n<p>In the code, we create one default <strong>Dog<\/strong> object and another object using the overloaded constructor.<\/p>\n<h3>Code Listing<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial.dog;\r\n\r\n\/\/Dog.java\r\n\/\/www.TestingDocs.com\r\n\r\npublic class Dog {\r\n  private String name;\r\n  private double weight;\r\n  private double height;\r\n\r\n  \/\/default constructor\r\n  public Dog() {\r\n    this.name = \"JustBorn\";\r\n    this.weight = 0.1;\r\n    this.height = 0.1;\r\n  }\r\n\r\n  \/\/ overloaded constructor\r\n  public Dog(String name, double weight, double height) {\r\n    this.name = name;\r\n    this.weight = weight;\r\n    this.height = height;\r\n  }\r\n\r\n  public String getName() {\r\n    return name;\r\n  }\r\n\r\n  public void setName(String name) {\r\n    this.name = name;\r\n  }\r\n  public double getWeight() {\r\n    return weight;\r\n  }\r\n\r\n  public void setWeight(double weight) {\r\n    this.weight = weight;\r\n  }\r\n\r\n  public double getHeight() {\r\n    return height;\r\n  }\r\n\r\n  public void setHeight(double height) {\r\n    this.height = height;\r\n  }\r\n\r\n  @Override\r\n  public String toString() {\r\n    return \"name=\" + name + \", weight=\" + weight + \", height=\" + height ;\r\n  }\r\n}\r\n<\/pre>\n<h3>Driver<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial.dog;\r\n\r\n\/*********************************************\r\n * DogDriver.java\r\n * www.TestingDocs.com\r\n * \r\n *********************************************\/\r\npublic class DogDriver {\r\n\r\n  public static void main(String[] args) {\r\n    Dog dogDefaultObject= new Dog();\r\n    Dog dogObject= new Dog(\"Tommy\",2.5,0.75);\r\n    System.out.println(\"Dog One=\" + dogDefaultObject.toString());\r\n    System.out.println(\"Dog Two=\" + dogObject.toString());\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Program Output<\/h3>\n<p>Dog One=name=JustBorn, weight=0.1, height=0.1<br \/>\nDog Two=name=Tommy, weight=2.5, height=0.75<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to overload a Constructor in Java In this tutorial, we will learn to overload constructor methods in Java with an example. We would take the Dog class as an example. We will create two constructors for the class. The default no-arg constructor and an overloaded constructor that accepts the object arguments in the parameters. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3832","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3832"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3832\/revisions"}],"predecessor-version":[{"id":24070,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3832\/revisions\/24070"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}