• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How does one implement and call one's own Functional Interface?

 
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To better understand Functional Interfaces, I was trying to understand from first principles.
In the OCP17 1Z-829 there is an example. I cannot figure out how one would call it as a lambda.






 
Saloon Keeper
Posts: 3946
43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply - am I understanding this correctly?
In the traditional way you are passing the parameter to the implementing class



But now you are invoking the method on the lambda expression while passing the parameter to it!

Mikalai Zaikin wrote:



It will be good if the book can contain this explanation.
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . In the traditional way you are passing the parameter to the implementing class

No, you are passing it as a method argument; you don't pass anything to a class.

. . . But now you are invoking the method on the lambda expression while passing the parameter to it!

No, you are creating an object of a class implementing Sprint and then passing the 10 as an argument to its method.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . I was trying to understand from first principles. . . .

I would suggest you try a book like Modern Java in Action by Urma, Fusco, and Mycroft (Manning, 2017 and earlier editions). That gives a lot of explanation of λs from the ground up.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Anil Philip wrote:. . . In the traditional way you are passing the parameter to the implementing class

No, you are passing it as a method argument; you don't pass anything to a class.



Agreed; that is what I meant. We are passing the parameter to the method on the Tiger class.

Campbell Ritchie wrote:

Anil Philip wrote:. . . But now you are invoking the method on the lambda expression while passing the parameter to it!

No, you are creating an object of a class implementing Sprint and then passing the 10 as an argument to its method.



So Java is creating an anonymous class instance of the Sprint interface?
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . . We are passing the parameter to the method on the Tiger class. . . .

No, you are passing the argument to the method of the Tiger object.

So Java is creating an anonymous class instance of the Sprint interface?

Sort of. It creates a class representing that Sprint object, rather like what an anonymous class used to be. Don't say, “class instance;” say, “instance.”
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Anil Philip wrote:. . . I was trying to understand from first principles. . . .

I would suggest you try a book like Modern Java in Action by Urma, Fusco, and Mycroft (Manning, 2017 and earlier editions). That gives a lot of explanation of λs from the ground up.



Thanks. The Boyarsky book does not cover rolling ones own Functional Interface and calling it.
I looked up the book you recommend - it was not updated after Java 9

Quite frankly, this Java certification has been draining the life of me.


 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . I looked up the book you recommend - it was not updated after Java 9 . . .

So what? It is probably the best book for that particular purpose. λs have hardly changed since Java8. You will probably get an old edition second‑hand for less than the newer edition.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . Thanks.

That's a pleasure

The Boyarsky book does not cover rolling ones own Functional Interface and calling it. . . .

That is because such a question won't come up in the exam. I personally think, if you want to learn programming, it is better to enrol as a student at a proper course than think you can learn it for a cert. exam. Cert. exams test knowledge of the rules of Java® rather than programming best practice. But that doesn't mean that cert. exams are at all easy.
You create a functional interface the same way you create an ordinary interface. You simply have to count how many methods you need. Always test it with the @FunctionlInterface annotation. There is more useful information in the @FunctionalInterface API link and the JLS (=Java® Language Specification). Remember the documentation comments are the biggest and bestest part of any interface.
I think we have already told you, you don't call interfaces or classes, but you call methods (and constructors).
 
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:I looked up the book you recommend - it was not updated after Java 9


I have the book and it does include Java 10 as well, but Java 10 didn't add much related to functional interfaces.

If you are thinking of buying the book, the 2nd edition is currently offered at a discount if you purchase through the Manning web site (print-book and/or DRM-free ebook).
 
author & internet detective
Posts: 42003
911
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

Anil Philip wrote:Thanks. The Boyarsky book does not cover rolling ones own Functional Interface and calling it.


I don't understand. On page 426, the section is titled "Coding Functional Interfaces". It starts with



That's how to roll your functional interface. On page 421 (a few pages earlier), we do the same thing with this and show how to call it with a lambda.



And on page 429 (several pages later) we have and how to call it with a method reference.



Can you give an example of what you think should be covered? This seems pretty thorough to me.
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Can you give an example of what you think should be covered? This seems pretty thorough to me.


Jeanne, Thank you for looking at this.
It shows us how to define our own Functional interface (Sprint) and also the implementing class (Tiger).
But it leaves out 1) how to call it 2) what happens behind the scenes when it is called 3) how the lambda expressions are tied to it.

Only after reading Mikalai and Ritchie's posts above, I understood:
1) the lambda expressions that are created, represent the implementation of the method in the Functional Interface.
2) behind the scenes, for each and every lambda expression that is passed in, Java creates one anonymous class instance of the Functional Interface.
2) the programmer invokes the method defined in that Functional Interface, on this anonymous class instance.

I feel this was critical information that was missing in the explanation of lambdas. It is a gap.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . for each and every lambda expression that is passed in, Java creates one anonymous class instance of the Functional Interface. . . .

How do you it is one instance per λ? How do you know that instances are not cached and reused?

. . . critical information that was missing . . . It is a gap.

Is it? Do you really need to know that?
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Anil Philip wrote:. . . for each and every lambda expression that is passed in, Java creates one anonymous class instance of the Functional Interface. . . .

How do you it is one instance per λ? How do you know that instances are not cached and reused?



Conceptually, that is what it appears to be. Reuse and caching are just optimizations.

Campbell Ritchie wrote:

. . . critical information that was missing . . . It is a gap.

Is it? Do you really need to know that?



Most certainly. I was always bewildered by Lambdas without this information now filling the gap.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic