• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

"The compiler does not allow a cast from interface reference to..." OCPJSE11

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look this afirmation, "The compiler does not allow a cast from interface reference to a object reference if the object type does not implement the interface"

Look this code.



How this code compile, following this rule in OCP JSE 11 book??Of course, will throw a CastClassException.
 
Nicholas Barbosa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicholas Barbosa wrote:Look this afirmation, "The compiler does not allow a cast from interface reference to a object reference if the object type does not implement the interface"

Look this code.



How this code compile, following this rule in OCP JSE 11 book??Of course, will throw a CastClassException.



I think, this follow the same rule as instanceof operator for interfaces.The compiller allows because the Concrete2 class its not final,so there is a possibility that yours child classes(of Concrete2) ,implement the InOne interface,making compatible.


This code compiles, however If I marked Concrete2 as final, never will compile.Because no one can extends Concrete2.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give us fuller details of the book, including authors. Is it Boyarsky and Selikoff?

The full details are in the Java® Language Specification (=JLS). The JLS and the language are written on the assumption that programmers are competent and when they wrote code the know what they are doing. If you write a narrowing reference conversion, it is conceivable that your no 2 class has a subtype implementing the InOne interface, so it “believes” you,even though you are “lying” to it with that cast.
 
Nicholas Barbosa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please give us fuller details of the book, including authors. Is it Boyarsky and Selikoff?

The full details are in the Java® Language Specification (=JLS). The JLS and the language are written on the assumption that programmers are competent and when they wrote code the know what they are doing. If you write a narrowing reference conversion, it is conceivable that your no 2 class has a subtype implementing the InOne interface, so it “believes” you,even though you are “lying” to it with that cast.



Yes, its OCP JSE11 book, from selikoff.

You said, what i meant in the previous comment. Java believes that a child class has a subtype implementing the InOne interface, thats why if I mark  the parent class as final, wont compile.

Look the example again.(works if concrete2 its not final)

 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds correct, but the common practice of saying, “child,” or “parent,” is misleading. The best terminology I have heard is what they say in C#: “base,” and, “derived,” classes.
 
author & internet detective
Posts: 42163
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. it should say "cannot implement" rather than "does not"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic