{"id":3689,"date":"2017-07-23T09:17:50","date_gmt":"2017-07-23T09:17:50","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3689"},"modified":"2024-12-14T04:52:43","modified_gmt":"2024-12-14T04:52:43","slug":"method-overloading-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/method-overloading-in-java\/","title":{"rendered":"Method Overloading in Java"},"content":{"rendered":"<h2>Method Overloading in Java<\/h2>\n<p>Method overloading in Java is a feature that allows a class to have more than one method having the same name, with different method signatures. It is similar to constructor overloading in java which allows a class to have more than one constructor having different argument lists.<\/p>\n<p><strong>Method signature = method return type + method name + method parameters<\/strong><\/p>\n<p>For example, if we take the argument list having two parameters i.e <strong>add(int a, int b)<\/strong> is different from the argument list with three parameters i.e <strong>add(int a, int b, int c).<\/strong><\/p>\n<p>They are two ways of method overloading<\/p>\n<p>1.Changing the number of arguments<\/p>\n<p>2.Changing the return data type<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3740\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Overloading-and-Overriding-in-Java.png\" alt=\"Overloading and Overriding in Java\" width=\"1734\" height=\"890\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Overloading-and-Overriding-in-Java.png 1734w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Overloading-and-Overriding-in-Java-300x154.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Overloading-and-Overriding-in-Java-1024x526.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Overloading-and-Overriding-in-Java-768x394.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Overloading-and-Overriding-in-Java-1536x788.png 1536w\" sizes=\"auto, (max-width: 1734px) 100vw, 1734px\" \/><\/p>\n<h3><\/h3>\n<h3>Code Listing<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial;\r\n\r\n\/\/MethodOverloadingDemo.java\r\npublic class MethodOverloadingDemo {\r\n  public static void main(String[] args) {\r\n    MethodOverloadingDemo mDemo = new MethodOverloadingDemo();\r\n    System.out.println(\"Integer overloaded method =\"+ mDemo.add(1, 5));\r\n    System.out.println(\"Double  overloaded method =\"+ mDemo.add(2.5, 3.5));\r\n    System.out.println(\"String overloaded method =\"+ mDemo.add(\"Testing\", \"Docs\"));\r\n  }\r\n  \r\n  \/\/Overloaded methods\r\n  public int add(int a,int b) {\r\n    return a+b;\r\n  }\r\n  \r\n  public double add(double a,double b) {\r\n    return a+b;\r\n  }\r\n  \r\n  public String add(String s1,String s2) {\r\n    return s1.concat(s2);\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3696\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Program-Output-Method-Overloading.png\" alt=\"Program Output Method Overloading\" width=\"1729\" height=\"1031\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Program-Output-Method-Overloading.png 1729w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Program-Output-Method-Overloading-300x179.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Program-Output-Method-Overloading-1024x611.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Program-Output-Method-Overloading-768x458.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Program-Output-Method-Overloading-1536x916.png 1536w\" sizes=\"auto, (max-width: 1729px) 100vw, 1729px\" \/><\/p>\n<p><strong>Sample Output<\/strong><\/p>\n<p>Integer overloaded method =6<br \/>\nDouble overloaded method =6.0<br \/>\nString overloaded method =TestingDocs<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Method Overloading in Java Method overloading in Java is a feature that allows a class to have more than one method having the same name, with different method signatures. It is similar to constructor overloading in java which allows a class to have more than one constructor having different argument lists. Method signature = method [&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-3689","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\/3689","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=3689"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3689\/revisions"}],"predecessor-version":[{"id":26433,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3689\/revisions\/26433"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}