{"id":327,"date":"2025-06-30T07:13:21","date_gmt":"2025-06-30T07:13:21","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=327"},"modified":"2025-07-16T10:12:08","modified_gmt":"2025-07-16T10:12:08","slug":"difference-between-overloading-and-overriding","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/difference-between-overloading-and-overriding\/","title":{"rendered":"Difference between overloading and overriding?"},"content":{"rendered":"\n<p>\ud83d\udd01 <strong>Method Overloading<\/strong> (Compile-Time Polymorphism)<\/p>\n\n\n\n<p><strong>\u2728 What It Is:<\/strong><\/p>\n\n\n\n<p>Overloading occurs when <strong>multiple methods have the same name<\/strong> but <strong>different parameter lists<\/strong>. These methods can exist in the same class or in a subclass.<\/p>\n\n\n\n<p><strong>\u2705 Key Rules:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Must have different number, type, or order of parameters.<\/li>\n\n\n\n<li>Can have different return types.<\/li>\n\n\n\n<li>Happens at compile time (the compiler decides which version to call based on the arguments).<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator {\n    int add(int a, int b) {\n        return a + b;\n    }\n\n    double add(double a, double b) {\n        return a + b;\n    }\n\n    int add(int a, int b, int c) {\n        return a + b + c;\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Calculator c = new Calculator();\n        System.out.println(c.add(2, 3));          \/\/ Output: 5\n        System.out.println(c.add(2.5, 3.5));      \/\/ Output: 6.0\n        System.out.println(c.add(1, 2, 3));       \/\/ Output: 6\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udd04 <strong>Method Overriding<\/strong> (Runtime Polymorphism)<\/p>\n\n\n\n<p>\u2728 What It Is:<\/p>\n\n\n\n<p>Overriding occurs when a <strong>subclass provides a specific implementation<\/strong> of a method that is already defined in its superclass.<\/p>\n\n\n\n<p>\u2705 Key Rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Method name, return type, and parameters must match exactly.<\/li>\n\n\n\n<li>Must occur across parent-child class relationship.<\/li>\n\n\n\n<li>Happens at runtime (which method gets called depends on the object&#8217;s actual type).<\/li>\n\n\n\n<li>Use <code>@Override<\/code> annotation to make it explicit.<\/li>\n\n\n\n<li>Access modifier can&#8217;t be more restrictive than in the parent.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd27 Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void makeSound() {\n        System.out.println(\"Animal makes a sound\");\n    }\n}\n\nclass Dog extends Animal {\n    @Override\n    void makeSound() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Animal myDog = new Dog();\n        myDog.makeSound();  \/\/ Output: Dog barks\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\ud83e\udde0 Comparison Table<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Method Overloading<\/th><th>Method Overriding<\/th><\/tr><\/thead><tbody><tr><td>Class Relationship<\/td><td>Happens in the same class<\/td><td>Requires inheritance (parent-child)<\/td><\/tr><tr><td>Method Signature<\/td><td>Must be different (parameters)<\/td><td>Must be exactly same<\/td><\/tr><tr><td>Return Type<\/td><td>Can vary<\/td><td>Must be same or covariant (subtype)<\/td><\/tr><tr><td>Access Modifier<\/td><td>Any (private, public, etc.)<\/td><td>Cannot be more restrictive than superclass<\/td><\/tr><tr><td>Static Methods<\/td><td>Can be overloaded<\/td><td>Cannot be overridden (can only be hidden)<\/td><\/tr><tr><td>Polymorphism Type<\/td><td>Compile-time polymorphism<\/td><td>Runtime polymorphism<\/td><\/tr><tr><td>Binding Time<\/td><td>Decided at compile time<\/td><td>Decided at runtime<\/td><\/tr><tr><td>Annotation Use<\/td><td>Optional<\/td><td>Recommended to use <code>@Override<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discover the key differences between method overloading and method overriding in Java. Learn how they support compile-time and runtime polymorphism<\/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-327","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\/327","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=327"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/327\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/327\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}