{"id":229,"date":"2025-06-24T07:27:13","date_gmt":"2025-06-24T07:27:13","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=229"},"modified":"2025-07-16T10:26:42","modified_gmt":"2025-07-16T10:26:42","slug":"what-is-the-method-overloading","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-the-method-overloading\/","title":{"rendered":"What is the Method Overloading?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>method overloading<\/strong> means having <strong>multiple methods in the same class with the same name but different parameters<\/strong>. It lets you write code that is more flexible and easier to read.<\/h3>\n\n\n\n<p>\ud83d\udcda Why Overload a Method?<\/p>\n\n\n\n<p>Sometimes, you want a method to do <strong>similar things<\/strong> but accept <strong>different types or numbers of inputs<\/strong>. Method overloading helps you achieve this without creating different method names for each version.<\/p>\n\n\n\n<p>\u2705 Rules for Method Overloading:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Same method name<\/li>\n\n\n\n<li>Different parameter list (type, number, or order)<\/li>\n\n\n\n<li>Can have different return types\u2014but return type alone is <em>not enough<\/em><\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd27 Example:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>class Calculator {<br>\/\/ Method to add two integers<br>int add(int a, int b) {<br>return a + b;<br>}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Method to add three integers\nint add(int a, int b, int c) {\n    return a + b + c;\n}\n\n\/\/ Method to add two doubles\ndouble add(double a, double b) {\n    return a + b;\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>public class Main {<br>public static void main(String[] args) {<br>Calculator calc = new Calculator();<br>System.out.println(calc.add(10, 20)); \/\/ Calls the first method<br>System.out.println(calc.add(10, 20, 30)); \/\/ Calls the second<br>System.out.println(calc.add(5.5, 4.5)); \/\/ Calls the third<br>}<br>}<\/p>\n<\/div><\/div>\n\n\n\n<p><strong>\ud83d\udda8 Output:<\/strong><\/p>\n\n\n\n<p>30 60 10.0<\/p>\n\n\n\n<p>Each <code>add()<\/code> method is tailored to different inputs\u2014same name, different behavior.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn what method overloading is in Java. Discover how defining multiple methods with the same name but different parameters improves flexibility and readability<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-229","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":3,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions"}],"predecessor-version":[{"id":452,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions\/452"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}