{"id":261,"date":"2014-04-05T13:50:49","date_gmt":"2014-04-05T08:20:49","guid":{"rendered":"http:\/\/saurabhjain1537.wordpress.com\/?p=261"},"modified":"2025-02-27T07:46:43","modified_gmt":"2025-02-27T07:46:43","slug":"oop-fundamentals-3-of-3","status":"publish","type":"post","link":"https:\/\/saurabhjain.dev\/?p=261","title":{"rendered":"OOP Fundamentals (3 of 3)"},"content":{"rendered":"<p>Polymorphism is the third essential feature of OOP.<\/p>\n<p>Polymorphism means many forms &#8211; an object can behave in different forms and the correct behavior will be evaluated during run-time only.<\/p>\n<p><strong>Upcasting<\/strong><\/p>\n<p>Taking the concept of inheritance further,\u00a0 an object of derived class can be used as its own type or as a type of its base class. Although this statement might appear as incorrect &#8211; why would Java allow one type to behave like another type, but if we think in a object oriented manner it will make sense.<\/p>\n<p>Inheritance is making a base class (generic) into a more specific derived class. So, derived class object still posses all the generic behavior of base class. By doing upcasting we are loosing specifics of a derived class but retaining generics of base class.<\/p>\n<p>Let&#8217;s take this example<\/p>\n<p><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/04\/upcast.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-264\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/04\/upcast.jpg?w=300\" alt=\"upcast\" width=\"536\" height=\"211\" \/><\/a><\/p>\n<p>Output :<\/p>\n<p><em>Car:Move<\/em><\/p>\n<p>Although this is the desired output, but looking at the method park() it doesn&#8217;t make sense. In the park() method we are invoking move() of Vehicle but actually move() of Car is invoked.<\/p>\n<p><strong>Dynamic Binding<\/strong><\/p>\n<p>Connecting a method call to method body is called binding. When this binding happens during compile time, its called <em>static binding<\/em>. When this binding happens during run time (based on type of object), its called <em>dynamic binding<\/em> or <em>runtime binding<\/em>.<\/p>\n<p>In the above example, even when Car object is upcasted to Vehicle in park(), it still knows its type information and behave in polymorphic manner to produce desired output.<\/p>\n<p><strong>Downcasting<\/strong> is opposite of upcasting. Here we cast a generic object into more specific one. While Upcasting is always safe and happens implicitly, Downcasting requires explicit type casting operator and throws a runtime ClassCastException if the instance to be downcasted does not belong to the correct subclass.<\/p>\n<p><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/04\/downcast.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-269\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/04\/downcast.jpg?w=300\" alt=\"downcast\" width=\"462\" height=\"77\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism is the third essential feature of OOP. Polymorphism means many forms &#8211; an object can behave in different forms and the correct behavior will be evaluated during run-time only. Upcasting Taking the concept of inheritance further,\u00a0 an object of derived class can be used as its own type or as a type of its &hellip; <a href=\"https:\/\/saurabhjain.dev\/?p=261\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">OOP Fundamentals (3 of 3)<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[20,21,42,43,47,62],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-java","tag-downcasting","tag-dynamic-binding","tag-object-oriented-programming","tag-oop","tag-polymorphism","tag-upcasting"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":1,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":787,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions\/787"}],"wp:attachment":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}