{"id":4501,"date":"2014-03-30T12:09:50","date_gmt":"2014-03-30T12:09:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/polymorphism-define-in-just-two-sentences-collection-of-common-programming-errors\/"},"modified":"2014-03-30T12:09:50","modified_gmt":"2014-03-30T12:09:50","slug":"polymorphism-define-in-just-two-sentences-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/polymorphism-define-in-just-two-sentences-collection-of-common-programming-errors\/","title":{"rendered":"Polymorphism &#8211; Define In Just Two Sentences-Collection of common programming errors"},"content":{"rendered":"<p>Just thought I&#8217;d add my own interpretation of what polymorphism is: very generically, polymorphism is the act of providing a <em>single interface<\/em> to entities of <em>different types<\/em>.<\/p>\n<p>That&#8217;s rather generic, but that&#8217;s the only way I can think of to wrap all three types of polymorphisms I know about: <em>ad hoc<\/em>, <em>parametric<\/em> and <em>subtype<\/em>. I&#8217;ll go in more details below, and have sorted polymorphism types by name, alphabetically. The one you&#8217;re interested on is most probably <em>subtype polymorphism<\/em>, which is the last one.<\/p>\n<h2>ad hoc polymorphism<\/h2>\n<p><em>ad hoc polymorphism<\/em> is the act of providing multiple implementations of the same method for different parameter types. In OOP, it&#8217;s generally known as <em>method overloading<\/em>. For example:<\/p>\n<pre><code>public String format(int a) {\n    return String.format(\"%2d\", a);\n}\n\npublic String format(Date a) {\n    return new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss'Z'\").format(a);\n}\n<\/code><\/pre>\n<p>Both <code>format<\/code> methods share a <em>single interface<\/em> but work on entities of <em>different types<\/em>.<\/p>\n<h2>Parametric polymorphism<\/h2>\n<p><em>Parametric polymorphism<\/em> is the act of making a class (or method) work on a type that is itself a parameter of the class (or method). It&#8217;s often referred to as <em>generics<\/em>.<\/p>\n<p>For example, Java&#8217;s <code>List[T]<\/code> expects a parameter <code>T<\/code> at instantiation time, and this parameter defines the type of the resulting object.<\/p>\n<p>Note for purists that I&#8217;m purposefully ignoring <em>raw types<\/em> as I feel they&#8217;d just muddy the waters in this context.<\/p>\n<p><code>List[String]<\/code> and <code>List[Date]<\/code> share a <em>single interface<\/em>, but work on (and are) <em>different types<\/em>.<\/p>\n<h2>Subtype polymorphism<\/h2>\n<p><em>Subtype polymorphism<\/em> is probably what you initially meant in your question: it&#8217;s the act of providing a single interface to multiple implementations of the same type.<\/p>\n<p>To use the customary example: <code>Animal<\/code> provides a contract that all implementations must respect. <code>Dog<\/code> is an <code>Animal<\/code>, and as such supports all operations that <code>Animal<\/code> declares. According to the Liskov substitution principle, this allows you to use an instance of <code>Dog<\/code> where an instance of <code>Animal<\/code> is expected (but not the other way around).<\/p>\n<p>If <code>Cat<\/code> and <code>Dog<\/code> are both subclasses of <code>Animal<\/code>, then they share a <em>single interface<\/em> but are in fact <em>different types<\/em>.<\/p>\n<p>I&#8217;m going off in a bit of a tangent here, but subtype polymorphism is (I think) the only one that allows <em>overriding<\/em>: the act of redefining the behaviour of a method defined by a parent class. This is often confused with <em>overloading<\/em> which, as we saw before, is a type of polymorphism and doesn&#8217;t in fact need subclassing (nor does it need classes, really).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just thought I&#8217;d add my own interpretation of what polymorphism is: very generically, polymorphism is the act of providing a single interface to entities of different types. That&#8217;s rather generic, but that&#8217;s the only way I can think of to wrap all three types of polymorphisms I know about: ad hoc, parametric and subtype. I&#8217;ll [&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-4501","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4501","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=4501"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4501\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}