• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting interfaces (Java OCA 8 Programmer I Study Guide, Sybex)

 
Fabien Lanier
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

There is a question in the practice exam #2 I do not understand. Here is the code:



I do not understand why the line #9 compiles.
How can we cast between 2 interface references since the interfaces do not seem to be related and the compiler does not know which object is referred by canClimb?
Can we cast any object referred by an interface reference to any other interface reference?

Thanks for your explanation!
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabien Lanier wrote:
I do not understand why the line #9 compiles.
How can we cast between 2 interface references since the interfaces do not seem to be related and the compiler does not know which object is referred by canClimb?
Can we cast any object referred by an interface reference to any other interface reference?


Well, it is an explicit cast, so, unless it is one of the few cases (specified in the JLS) that is not allowed, the compiler will allow it. And of course, if the compiler is wrong, then it will fail at runtime.

Henry
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, exactly because you're casting it to an interface. The compiler can't reason that the object doesn't implement that interface (indeed, in this case the thing that can climb also has claws!), so it allows the cast. If the object doesn't implement the interface, it will throw a ClassCastException at runtime.

This is not the case if you're trying to cast a class to a different class rather than an interface. The compiler is smart enough to know when an object reference *really* can't be cast to a certain class. For instance, a String can *never* be cast to a Number.

In short, casting to and from an interface is always allowed.
 
Fabien Lanier
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:In short, casting to and from an interface is always allowed.


OK I get it, thanks!

The explanation for the exam answer is not very clear though. It says:

Line 9 also compiles correctly since they are related types, so option E is incorrect.


Can we say two interfaces are related types if they do not extend each other?
 
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
Hi Fabien Lanier,

First of all, a warm welcome to CodeRanch!

Fabien Lanier wrote:I do not understand why the line #9 compiles.
How can we cast between 2 interface references since the interfaces do not seem to be related and the compiler does not know which object is referred by canClimb?
Can we cast any object referred by an interface reference to any other interface reference?

The exact same code snippet is also disccused in this topic and this one. In the latter one also other code snippets are discussed and explained. Both are definitely worth reading.

Hope it helps!
Kind regards,
Roel
 
Fabien Lanier
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

You were right these posts were definitely worth reading, I do have a much better understanding of the topic now!
Next time I will do a better search in the forum I promise.

Thanks!

By the way a big THANK YOU to Jeanne Boyarsky, Scott Selikoff and everyone who participated in creating the OCA book, I passed successfully my OCA exam yesterday without using anything else than the book!
 
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
Stephan van Hulst wrote:In short, casting to and from an interface is always allowed.

Unfortunately that's incorrect!

Casting to an interface is not always allowed. Considering the following syntax for a cast
and DestinationType is an interface, you have three different cases:
  • the type of sourceTypeReference is also an interface => the cast is always allowed
  • the type of sourceTypeReference is a non-final class => the cast is always allowed
  • the type of sourceTypeReference is a final class => the cast is only allowed if the type of sourceTypeReference implements DestinationType; otherwise you'll get a compiler error

  • This code snippet illustrates these different casesIt should be pretty obvious that case1 and case2 will throw a ClassCastException at runtime. Only case3 will compile and execute without any errors.

    Hope it helps!
    Kind regards,
    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
    Fabien Lanier wrote:By the way a big THANK YOU to Jeanne Boyarsky, Scott Selikoff and everyone who participated in creating the OCA book, I passed successfully my OCA exam yesterday without using anything else than the book!

    Congratz! (I'll reserve the fancy congratulations for when you have shared your experiences )
     
    Stephan van Hulst
    Bartender
    Pie
    Posts: 6503
    83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're correct Roel, I forgot about final classes.

    Fabien, congratulations!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic