{"id":497,"date":"2013-03-28T03:10:54","date_gmt":"2013-03-28T03:10:54","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=497"},"modified":"2013-03-28T03:10:54","modified_gmt":"2013-03-28T03:10:54","slug":"inheritance-vs-polymorphism","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/inheritance-vs-polymorphism\/","title":{"rendered":"Inheritance vs Polymorphism"},"content":{"rendered":"<p>Inheritance refers to using the structure and behavior of a superclass in a subclass. Polymorphism refers to changing the behavior of a superclass in the subclass.v<\/p>\n<p>Inheritance is more a static thing (one class extends another) while polymorphism is a dynamic\/ runtime thing (an object behaves according to its dynamic\/ runtime type not to its static\/ declaration type).<\/p>\n<p>Inheritance is when a &#8216;class&#8217; derives from an existing &#8216;class&#8217;. So if you have a Person class, then you have a Student class that extends Person, Student inherits all the things that Person has. <\/p>\n<p>Polymorphism deals with how the program decides which methods it should use, depending on what type of thing it has. If you have a Person, which has a read method, and you have a Student which extends Person, which has its own implementation of read, which method gets called is determined for you by the runtime, depending if you have a Person or a Student<\/p>\n<p>Person p = new Student();<br \/>\np.read();<\/p>\n<p>The read method on Student gets called. Thats the polymorphism in action. You can do that assignment because a Student is a Person, but the runtime is smart enough to know that the actual type of p is Student.<\/p>\n<p>The main difference is polymorphism is a specific result of inheritance. Polymorphism is where the method to be invoked is determined at runtime based on the type of the object. This is a situation that results when you have one class inheriting from another and overriding a particular method. However, in a normal inheritance tree, you don&#8217;t have to override any methods and therefore not all method calls have to be polymorphic<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance refers to using the structure and behavior of a superclass in a subclass. Polymorphism refers to changing the behavior of a superclass in the subclass.v Inheritance is more a static thing (one class extends another) while polymorphism is a dynamic\/ runtime thing (an object behaves according to its dynamic\/ runtime type not to its&hellip; <a href=\"https:\/\/codethataint.com\/blog\/inheritance-vs-polymorphism\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-java-concepts"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=497"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}