• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • 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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel !!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic