{"id":685,"date":"2013-01-12T02:55:34","date_gmt":"2013-01-12T02:55:34","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=449"},"modified":"2016-08-24T08:34:56","modified_gmt":"2016-08-24T08:34:56","slug":"casting-subclass-to-superclass-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/casting-subclass-to-superclass-java\/","title":{"rendered":"casting subclass to superclass  java"},"content":{"rendered":"<p>The below code generates no compilation error in eclipse but throws error during Runtime.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Animal\r\n{\r\n  public void eat(){}\r\n}\r\npublic class Dog extends Animal\r\n{\r\n  public void eat(){}\r\n  public void main(String&#x5B;] args)\r\n  {\r\n    Animal animal=new Animal();\r\n    Dog dog=(Dog) animal;\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nException in thread \"main\" java.lang.ClassCastException: com.mugil.wild.Animal cannot be cast to com.mugil.wild.Dog\r\n\tat com.mugil.wild.Dog.main(Dog.java:12)\r\n\r\n<\/pre>\n<p>By using a cast you&#8217;re essentially telling the compiler &#8220;trust me. I&#8217;m a professional, I know what I&#8217;m doing and I know that although you can&#8217;t guarantee it, I&#8217;m telling you that this animal variable is definitely going to be a dog<\/p>\n<p>Because you&#8217;re essentially just stopping the compiler from complaining, every time you cast it&#8217;s important to check that you won&#8217;t cause a ClassCastException by using instanceof in an if statement.<\/p>\n<p>Generally, downcasting is not a good idea. You should avoid it. If you use it, you better include a check: <\/p>\n<p>Animal animal = new Dog();<\/p>\n<p>if (animal instanceof Dog)<br \/>\n{<br \/>\n    Dog dog = (Dog) animal;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The below code generates no compilation error in eclipse but throws error during Runtime. public class Animal { public void eat(){} } public class Dog extends Animal { public void eat(){} public void main(String&#x5B;] args) { Animal animal=new Animal(); Dog dog=(Dog) animal; } } Output Exception in thread &#8220;main&#8221; java.lang.ClassCastException: com.mugil.wild.Animal cannot be cast to&hellip; <a href=\"https:\/\/codethataint.com\/blog\/casting-subclass-to-superclass-java\/\">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":[99],"tags":[],"class_list":["post-685","post","type-post","status-publish","format-standard","hentry","category-classes-and-objects"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/685","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=685"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/685\/revisions"}],"predecessor-version":[{"id":1462,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/685\/revisions\/1462"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}