• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Object cast (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ranch Hand
Posts: 127
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,

I expected above code would throw compile time exceptions. However it compiled and printed "15true".
Here are my questions:-
1. Does Interface in java inherit from java.lang.Object?
2. If not, why does compiler sense an issue when Object "koala" (please see comment A) cast'ed to an interface reference variable next line (comment B) since they do not share same inheritance hierarchy?
3. Why does compiler not throw an error when reference object "canClimb" (line with comment B) cast'ed to another interface reference object HasClaws (line with comment C) since they do not share same inheritance hierarchy?

Please clarify.

Thanks,
Raghu
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raghavendra Desoju wrote:1. Does Interface in java inherit from java.lang.Object?


No! Only classes extend from java.lang.Object (directly or indirectly).

Raghavendra Desoju wrote:2. If not, why does compiler sense an issue when Object "koala" (please see comment A) cast'ed to an interface reference variable next line (comment B) since they do not share same inheritance hierarchy?


The compiler knows that reference variable koala is of type Object. Because class Object is not final, you can cast a reference variable to any interface without a compiler error (you might get a runtime ClassCastException if the interface is incompatible). If class Object would have been final, you'll get a compiler error on lineB. A more detailed explanation about casting and interfaces can be found here.

Raghavendra Desoju wrote:3. Why does compiler not throw an error when reference object "canClimb" (line with comment B) cast'ed to another interface reference object HasClaws (line with comment C) since they do not share same inheritance hierarchy?


The compiler knows that reference variable canClimb is of type CanClimb. Because interface CanClimb is an interface (and therefore not final), you can cast a reference variable to any interface without a compiler error (you might get a runtime ClassCastException if the interface is incompatible). You can also cast to a any non-final class (you might get a runtime ClassCastException if the class is incompatible). You can even cast to a final class, but only if this class implements CanClimb; otherwise you'll get a compiler error.
Let's provide some code snippets for all these different cases

Hope it helps!
Kind regards,
Roel

[edit] fixed a mistake (it's getting late ): you don't get a compiler error when casting canClimb to final class Ant (because it implements the CanClimb interface), but you'll get a runtime ClassCastException because Koala (the actual object canClimb is refering to) IS-NOT-A Ant.
 
Raghavendra Desoju
Ranch Hand
Posts: 127
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Roel !!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just noticed I made a mistake in one of the code snippets. I edited my post and fixed it!
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic