• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Generic abstract method & Functional interface & lambda expression

 
Ranch Hand
Posts: 119
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: If I use a lambda expression with functional interface, the abstract method in functional interface cannot have "independent" generic parameter type on its own?



Example 1:
The concept that I have is that when I have a method that declares a generic parameter type, it is independent of the class generics.
T in tricky() method is independent of the T in class Crate.



If I apply it to the functional interface and then use the lambda expression, it behaves interesting.

Example 2:
Compile and run successfully.
Variable m is a raw type. Therefore, Java doesn't check the generic requirement for the test() method. And the Object type is returned.




Now, I declare the variable m as the parameterized type of type MyInterface<Integer>.
Compiler generates error.
Example 3:




Finally, I take out the generic parameter type <T> from test() method. It works fine. Program compiles.
The abstract method "T test(T t)" is now dependent on the generic T of the interface MyInterface<T>.
Example 4:

 
Marshal
Posts: 26523
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It seems like writing that code which declares two different type variables with the same name is leading you into confusion. Perhaps if you write the exactly equivalent code which doesn't do that:

you might find it less confusing. Let us know, one way or the other.

 
henry leu
Ranch Hand
Posts: 119
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Yes, I understand that I could've written like you mentioned.


But I would like to know why I'm getting error on example 3.

Does that mean I cannot use lambda expression on a functional interface that has a "independent" generic method "public <T> T test(T t)"?
Does that mean I have to use lambda expression on a functional interface that has a "dependant" generic method "public T test(T t)"?
 
Saloon Keeper
Posts: 12878
279
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to use Paul's version of the type parameters, because it makes explanation easier to follow.

henry leu wrote:But I would like to know why I'm getting error on example 3.


You didn't specify the type argument for the type parameter Z, so your lambda expression returns a MyInterface<Object> which you are trying to assign to a MyInterface<Integer>. Java can't infer that Z should be Integer, because Z and T are not related to each other. If you want to assign Integer to type parameter Z, you must do it explicitly:


Does that mean I cannot use lambda expression on a functional interface that has a "independent" generic method "public <T> T test(T t)"?


No. It means you must explicitly specify the type arguments of the generic method.


Does that mean I have to use lambda expression on a functional interface that has a "dependant" generic method "public T test(T t)"?


No. I showed you how it can be done. However, you wouldn't really write a method like that in real life.

In real life, you would either use only the type parameter declared on the class, or you would establish a relationship between the type parameter of the class and the type parameter of the method:


[edit]

Sorry, I was confused. The answer is very simple: No, you can't use lambda expressions to create instances of functional interfaces that have a generic method.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic