• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Sierra/Bates/Robson OCP8 Exam Guide errata p497  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found another error on Sierra/Bates/Robson OCP 8 Programmer II Exam Guide, page 497, first paragraph.  Currently it says "If you use DogQuerier type to define a lambda expression and then later add another abstract method to this interface, without the @FunctionalInterface annotation, you'll get a compiler error, "The target type of this expression must be a functional interface." It should be corrected as "If you use DogQuerier type to define a lambda expression and then later add another abstract method to this interface, with the @FunctionalInterface annotation, you'll get a compiler error, "The target type of this expression must be a functional interface."
 
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I couldn't find such an erratum mentioned here; I presume you have already looked at that link. Please post more details about the question; I don't have that book. Please also confirm which version of Java┬« you are using; the error message might be different in Java8, Java9 and Java10. The actual compiler error will differ depending on the order you compile the different types. If you have a functional interface and add a second abstract method to it, you will get different errors depending on whether you do or don't recompile the client code. If you don't recompile the client code, the compiler will probably not “notice” any errors at compile time. I don't know whether that will cause problems at runtime. If you add a second method after using the annotation, the interface will fail to compile with a different message, but the old .class file will be unchanged and I don't expect an error at runtime. The errors may also be different if the two types are in different packages.
 
Seevali Hewa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
I am referring to the book "OCP Java SE 8 Programmer II Exam Guide (Exam 1Z0-809) Completee Exam Preperation by Kathy Sierra, Bert Bates, Elisabeth Robinson". Here is the link to the book on Amazon. https://www.amazon.co.uk/Java-Programmer-Exam-Guide-1Z0-809/dp/1260117383/ref=sr_1_1?ie=UTF8&qid=1536612067&sr=8-1&keywords=ocp+java+se+8+kathy+sierra.

I use Java8 to compile the source. I believe you get the compilation error when you add more than one abstract method to an interface marked with @FunctionalInterface annotation. Not without the @FunctionalInterface. Correct me if I am wrong.

Thanks
 
Campbell Ritchie
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I think I was mistaken and I do have that book. I shall have another look at the question, but I think you have got with and without the annotation the wrong way round.
 
Campbell Ritchie
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my mistake; I have found the book and page 497, which says,

If you use the DogQuerier type to define a lambda expression, and then later add another abstract method to this interface, without the @FunctionalInterface annotation you'll either get a compiler error, "The target type of this expression must be a functional interface," or a runtime error.

So, let's have a functional interface:-Because the variegate() method is commented‑out, that is a bona fide functional interface; if you compile it with javac functional2/Supplier.java it will compile. If you comment the variegate() method back in, it ceases to be a functional interface. and with the annotation, you get a compiler error

The javac tool in Java10 wrote:javac functional2/Supplier.java
functional2/Supplier.java:3: error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  Supplier is not a functional interface
    multiple non-overriding abstract methods found in interface Supplier
1 error

If I comment the annotation out too, then Supplier will compile. My Client class in the functional package . . . now is faced with a non‑functional interface, and it will run as before. The thing is, the Client.class file has already compiled with the λs in place, so it will run without an error.

javac functional2/Supplier.java
java functional.Client 123 456
+ -> 579
- -> -333
* -> 56088
/ -> 0
% -> 123

Note the absence of error messages after the 1st line, which shows the code compiled. Note the absence of error messages after the 1st line, which shows the Supplier code compiled. But I cannot recompile Client.

javac functional/Client.java
functional/Client.java:27: error: incompatible types: Supplier is not a functional interface
        Supplier add = (i, j) -> i + j;
                       ^
    multiple non-overriding abstract methods found in interface Supplier
functional/Client.java:28: error: incompatible types: Supplier is not a functional interface
        Supplier sub = (i, j) -> i - j;
                       ^
    multiple non-overriding abstract methods found in interface Supplier
functional/Client.java:29: error: incompatible types: Supplier is not a functional interface
        Supplier mul = (i, j) -> i * j;
                       ^
    multiple non-overriding abstract methods found in interface Supplier
functional/Client.java:30: error: incompatible types: Supplier is not a functional interface
        Supplier div = (i, j) -> i / j;
                       ^
    multiple non-overriding abstract methods found in interface Supplier
functional/Client.java:31: error: incompatible types: Supplier is not a functional interface
        Supplier rem = (i, j) -> i % j;
                       ^
    multiple non-overriding abstract methods found in interface Supplier
5 errors

You will notice that the error messages are slightly different in Java10, but they mean the same thing: I am trying to use a non‑functional interface to create a λ. But I still have the old version of Client.class, which I can still execute.

I still think the parts about without the annotation and with the annotation in the book are correct. But I haven't produced a runtime error.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!