• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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: 41
  • Mark post as helpful
  • send pies
  • 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: 71629
312
  • Mark post as helpful
  • send pies
  • 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: 41
  • Mark post as helpful
  • send pies
  • 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: 71629
312
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 40354
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. it should say "cannot implement" rather than "does not"
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic