{"id":4428,"date":"2014-03-30T10:54:58","date_gmt":"2014-03-30T10:54:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/overloading-is-compile-time-polymorphism-really-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:54:58","modified_gmt":"2014-03-30T10:54:58","slug":"overloading-is-compile-time-polymorphism-really-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/overloading-is-compile-time-polymorphism-really-collection-of-common-programming-errors\/","title":{"rendered":"Overloading is compile-time polymorphism. Really?-Collection of common programming errors"},"content":{"rendered":"<p>I do know the syntactical difference between overriding and overloading. And I also know that overriding is run-time polymorphism and overloading is compile-time polymorphism. But my question is: &#8220;Is overloading is really compile-time polymorphism? Is the method call really solving at compile time?&#8221;. To clarify my point, let&#8217;s consider an example class.<\/p>\n<pre><code>public class Greeter {\n    public void greetMe() {\n        System.out.println(\"Hello\");\n    }\n\n    public void greetMe(String name) {\n        System.out.println(\"Hello \" + name);\n    }\n\n    public void wishLuck() {\n        System.out.println(\"Good Luck\");\n    }\n}\n<\/code><\/pre>\n<p>Since all of the methods <code>greetMe(), greetMe(String name), wishLuck()<\/code> are public, they all can be overriden(including overloaded one), right? For example,<\/p>\n<pre><code>public class FancyGreeter extends Greeter {\n    public void greetMe() {\n        System.out.println(\"***********\");\n        System.out.println(\"*  Hello  *\");\n        System.out.println(\"***********\");\n    }\n}\n<\/code><\/pre>\n<p>Now, consider the following snippet:<\/p>\n<pre><code>Greeter greeter = GreeterFactory.getRandomGreeter();\ngreeter.greetMe();\n<\/code><\/pre>\n<p>The <code>getRandomGreeter()<\/code> method returns a random <code>Greeter<\/code> object. It may either return an object of <code>Greeter<\/code>, or any of its subclasses, like <code>FancyGreeter<\/code> or <code>GraphicalGreeter<\/code> or any other one. The <code>getRandomGreeter()<\/code> will create the objects either using <code>new<\/code> or dynamically load the class file and create object using reflection(I think it is possible with reflection) or any other way that is possible. All of these methods of <code>Greeter<\/code> may or may not be overriden in subclasses. So the compiler has no way to know whether a particular method(overloaded or not) is overriden. Right? Also, wikipedia says on Virtual functions:<\/p>\n<pre><code>In Java, all non-static methods are by default \"virtual functions\". \nOnly methods marked with the keyword final, which cannot be overridden, \nalong with private methods, which are not inherited, are non-virtual.\n<\/code><\/pre>\n<p>Since, virtual functions are resolved at run-time using dynamic method dispatch, and since all non private, non final methods are virtual(whether overloaded or not), they must be resolved at run-time. Right?<\/p>\n<p>Then, How can overloading still be resolved at compile-time? Or, is there anything that I misunderstood, or am I missing?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I do know the syntactical difference between overriding and overloading. And I also know that overriding is run-time polymorphism and overloading is compile-time polymorphism. But my question is: &#8220;Is overloading is really compile-time polymorphism? Is the method call really solving at compile time?&#8221;. To clarify my point, let&#8217;s consider an example class. public class Greeter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4428","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4428"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4428\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}