{"id":1185,"date":"2022-02-24T20:08:08","date_gmt":"2022-02-24T20:08:08","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1185"},"modified":"2022-02-24T20:08:08","modified_gmt":"2022-02-24T20:08:08","slug":"method-overloading","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/method-overloading\/","title":{"rendered":"Method Overloading"},"content":{"rendered":"\n<p>In this article, you\u2019ll learn about method overloading and how you can achieve it in Java with the help of examples.<\/p>\n\n\n\n<p>In Java, two or more\u00a0methods\u00a0may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func() { ... }\nvoid func(int a) { ... }\nfloat func(double a) { ... }\nfloat func(int a, float b) { ... }<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>func()<\/code>&nbsp;method is overloaded. These methods have the same name but accept different arguments.<\/p>\n\n\n\n<p><strong>Note<\/strong>: The return types of the above methods are not the same. It is because method overloading is not associated with return types. Overloaded methods may have the same or different return types, but they must differ in parameters.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>Why method overloading?<\/h2>\n\n\n\n<p>Suppose, you have to perform the addition of given numbers but there can be any number of arguments (let\u2019s say either 2 or 3 arguments for simplicity).<\/p>\n\n\n\n<p>In order to accomplish the task, you can create two methods&nbsp;<code>sum2num(int, int)<\/code>&nbsp;and&nbsp;<code>sum3num(int, int, int)<\/code>&nbsp;for two and three parameters respectively. However, other programmers, as well as you in the future may get confused as the behavior of both methods are the same but they differ by name.<\/p>\n\n\n\n<p>The better way to accomplish this task is by overloading methods. And, depending upon the argument passed, one of the overloaded methods is called. This helps to increase the readability of the program.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How to perform method overloading in Java?<\/h2>\n\n\n\n<p>Here are different ways to perform method overloading:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>1. Overloading by changing the number of parameters<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class MethodOverloading {\n    private static void display(int a){\n        System.out.println(\"Arguments: \" + a);\n    }\n\n    private static void display(int a, int b){\n        System.out.println(\"Arguments: \" + a + \" and \" + b);\n    }\n\n    public static void main(String&#91;] args) {\n        display(1);\n        display(1, 4);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Arguments: 1\nArguments: 1 and 4<\/samp><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>2. Method Overloading by changing the data type of parameters<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class MethodOverloading {\n\n    \/\/ this method accepts int\n    private static void display(int a){\n        System.out.println(\"Got Integer data.\");\n    }\n\n    \/\/ this method  accepts String object\n    private static void display(String a){\n        System.out.println(\"Got String object.\");\n    }\n\n    public static void main(String&#91;] args) {\n        display(1);\n        display(\"Hello\");\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Got Integer data.\nGot String object.<\/samp><\/code><\/pre>\n\n\n\n<p>Here, both overloaded methods accept one argument. However, one accepts the argument of type&nbsp;<code>int<\/code>&nbsp;whereas other accepts&nbsp;<code>String<\/code>&nbsp;object.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><a><\/a>Let\u2019s look at a real-world example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class HelperService {\n\n    private String formatNumber(int value) {\n        return String.format(\"%d\", value);\n    }\n\n    private String formatNumber(double value) {\n        return String.format(\"%.3f\", value);\n    }\n\n    private String formatNumber(String value) {\n        return String.format(\"%.2f\", Double.parseDouble(value));\n    }\n\n    public static void main(String&#91;] args) {\n        HelperService hs = new HelperService();\n        System.out.println(hs.formatNumber(500));\n        System.out.println(hs.formatNumber(89.9934));\n        System.out.println(hs.formatNumber(\"550\"));\n    }\n}<\/code><\/pre>\n\n\n\n<p>When you run the program, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>500\n89.993\n550.00<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Note<\/strong>: In Java, you can also overload constructors in a similar way like methods.<\/p>\n\n\n\n<p><strong>Recommended Reading:<\/strong>\u00a0Java Constructor Overloading<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a>Important Points<\/h3>\n\n\n\n<ul><li>Two or more methods can have the same name inside the same class if they accept different arguments. This feature is known as method overloading.<\/li><li>Method overloading is achieved by either:<ul><li>changing the number of arguments.<\/li><li>or changing the data type of arguments.<\/li><\/ul><\/li><li>It is not method overloading if we only change the return type of methods. There must be differences in the number of parameters.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you\u2019ll learn about method overloading and how you can achieve it in Java with the help of examples. In Java, two or more\u00a0methods\u00a0may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1185"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1185"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1185\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}