{"id":10311,"date":"2023-12-08T21:19:56","date_gmt":"2023-12-08T21:19:56","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=10311"},"modified":"2023-12-08T21:22:19","modified_gmt":"2023-12-08T21:22:19","slug":"late-and-early-binding-polymorphism","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/late-and-early-binding-polymorphism\/","title":{"rendered":"Late and Early binding polymorphism"},"content":{"rendered":"<p>I was taking part in an interview and was asked to explain &#8220;late and early binding polymorphism&#8221;. <\/p>\n<p>Now I&#8217;ve programmed in C++, Java, C# and other OO languages for many years, but I&#8217;ve have never (that I can recall) heard of late or early binding polymorphism. Safe to say, I needed to find out more&#8230;<\/p>\n<p><strong>Early Binding Polymorphism<\/strong><\/p>\n<p>Early (also known as static) binding is when we override methods in subclasses, i.e. these are resolved at compile time. <\/p>\n<p>So for example we have a Button which overrides the Click method and this is early or statically bound <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Window\r\n{\r\n  public virtual void Click()\r\n  {\r\n     \/\/ handles a click on the window\r\n  }\r\n}\r\n\r\npublic class Button : Window\r\n{\r\n  public override void Click()\r\n  {\r\n    \/\/ handles a button click\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Late Binding Polymorphism<\/strong><\/p>\n<p>Late (also known as dynamic and runtime) binding is when we assign a derived type to it&#8217;s base type. So using the previous example code the following would cause late binding to take place, i.e. the virtual method <em>Click<\/em> is resolved at runtime.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nWindows window = new Button();\r\nwindow.Click();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was taking part in an interview and was asked to explain &#8220;late and early binding polymorphism&#8221;. Now I&#8217;ve programmed in C++, Java, C# and other OO languages for many years, but I&#8217;ve have never (that I can recall) heard of late or early binding polymorphism. Safe to say, I needed to find out more&#8230; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[709],"tags":[],"class_list":["post-10311","post","type-post","status-publish","format-standard","hentry","category-object-oriented-design"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=10311"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10311\/revisions"}],"predecessor-version":[{"id":10315,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10311\/revisions\/10315"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=10311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=10311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=10311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}