{"id":5191,"date":"2014-03-30T19:33:20","date_gmt":"2014-03-30T19:33:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/need-clarification-about-inheritance-and-exceptions-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:33:20","modified_gmt":"2014-03-30T19:33:20","slug":"need-clarification-about-inheritance-and-exceptions-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/need-clarification-about-inheritance-and-exceptions-collection-of-common-programming-errors\/","title":{"rendered":"Need clarification about inheritance and exceptions-Collection of common programming errors"},"content":{"rendered":"<p>Other answers have explained the over-ridden method; however, there&#8217;s another issue. You said that:<\/p>\n<blockquote>\n<p>They are all instance of Car so what&#8217;s happening here?<\/p>\n<\/blockquote>\n<p>But to the compiler, they are not all instances of Car &#8211; the compiler will look at the type of the <em>variables<\/em>:<\/p>\n<pre><code>  Vehicle v = new Car();\n  Car c = new Car();\n  Vehicle c2 = (Vehicle) v;\n<\/code><\/pre>\n<p>At compile time, <code>v<\/code> is treated as a <code>Vehicle<\/code>, <code>c<\/code> as a <code>Car<\/code>, and the exceptions will be handled accordingly. At runtime, the JVM knows that <code>v<\/code> actually contains a <code>Car<\/code>, but that&#8217;s different.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Other answers have explained the over-ridden method; however, there&#8217;s another issue. You said that: They are all instance of Car so what&#8217;s happening here? But to the compiler, they are not all instances of Car &#8211; the compiler will look at the type of the variables: Vehicle v = new Car(); Car c = new [&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-5191","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5191","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=5191"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5191\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}