Lambda Expression JAVA-8
I have just started with JAVA 1.8 version and had a question while going through the tutorials regarding lambda expression.
Can we have more than 1 implementation(lambda expression) for the abstract method by creating multiple instances of the Interface WITHIN THE SAME CLASS???
I tried the code and it ran perfectly....
Now my question is that the very concept of interface is that every IMPLEMENTING CLASS WILL HAVE A DEFINITION FOR THE ABSTRACT METHOD. THEN HOW CAN WE HAVE TWO METHOD BODIES(lambda expressions) in the SAME CLASS ???
Consider the below code :
public static void main(String args) {
Interf i = (a, b) -> a + b;
System.out.println("The result is >> " + i.result(10, 20));
Interf i1 = (a, b) -> a - b;
System.out.println("The result is >> " + i1.result(10, 20));
}
Output:
The result is >> 30
The result is >> -10
lambda java-8
add a comment |
I have just started with JAVA 1.8 version and had a question while going through the tutorials regarding lambda expression.
Can we have more than 1 implementation(lambda expression) for the abstract method by creating multiple instances of the Interface WITHIN THE SAME CLASS???
I tried the code and it ran perfectly....
Now my question is that the very concept of interface is that every IMPLEMENTING CLASS WILL HAVE A DEFINITION FOR THE ABSTRACT METHOD. THEN HOW CAN WE HAVE TWO METHOD BODIES(lambda expressions) in the SAME CLASS ???
Consider the below code :
public static void main(String args) {
Interf i = (a, b) -> a + b;
System.out.println("The result is >> " + i.result(10, 20));
Interf i1 = (a, b) -> a - b;
System.out.println("The result is >> " + i1.result(10, 20));
}
Output:
The result is >> 30
The result is >> -10
lambda java-8
add a comment |
I have just started with JAVA 1.8 version and had a question while going through the tutorials regarding lambda expression.
Can we have more than 1 implementation(lambda expression) for the abstract method by creating multiple instances of the Interface WITHIN THE SAME CLASS???
I tried the code and it ran perfectly....
Now my question is that the very concept of interface is that every IMPLEMENTING CLASS WILL HAVE A DEFINITION FOR THE ABSTRACT METHOD. THEN HOW CAN WE HAVE TWO METHOD BODIES(lambda expressions) in the SAME CLASS ???
Consider the below code :
public static void main(String args) {
Interf i = (a, b) -> a + b;
System.out.println("The result is >> " + i.result(10, 20));
Interf i1 = (a, b) -> a - b;
System.out.println("The result is >> " + i1.result(10, 20));
}
Output:
The result is >> 30
The result is >> -10
lambda java-8
I have just started with JAVA 1.8 version and had a question while going through the tutorials regarding lambda expression.
Can we have more than 1 implementation(lambda expression) for the abstract method by creating multiple instances of the Interface WITHIN THE SAME CLASS???
I tried the code and it ran perfectly....
Now my question is that the very concept of interface is that every IMPLEMENTING CLASS WILL HAVE A DEFINITION FOR THE ABSTRACT METHOD. THEN HOW CAN WE HAVE TWO METHOD BODIES(lambda expressions) in the SAME CLASS ???
Consider the below code :
public static void main(String args) {
Interf i = (a, b) -> a + b;
System.out.println("The result is >> " + i.result(10, 20));
Interf i1 = (a, b) -> a - b;
System.out.println("The result is >> " + i1.result(10, 20));
}
Output:
The result is >> 30
The result is >> -10
lambda java-8
lambda java-8
asked Nov 15 '18 at 6:30
Lokesh BaranwalLokesh Baranwal
132
132
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Each of your two lambda expressions implements your Interf
functional interface separately. Therefore each implementation of that interface has a single implementation of that interface's single abstract method.
Even before Java 8 and lambda expressions you could create two anonymous class instances that implement your Interf
interface. Each one of them would have a single implementation of Interf
's single method.
The fact that the two lambda expressions are defined in the same class doesn't mean that the two implementations of the functional interface's abstract method belong to the same class.
add a comment |
Actually they are two different implementation of the same interface. In fact, you can use the existing BinaryOperator
to do the same rather than reinventing the wheel like so.
BinaryOperator<Integer> sum = (n1, n2) -> n1 + n2;
BinaryOperator<Integer> substract = (n1, n2) -> n1 - n2;
System.out.println(sum.apply(10, 20));
System.out.println(substract.apply(10, 20));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313656%2flambda-expression-java-8%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Each of your two lambda expressions implements your Interf
functional interface separately. Therefore each implementation of that interface has a single implementation of that interface's single abstract method.
Even before Java 8 and lambda expressions you could create two anonymous class instances that implement your Interf
interface. Each one of them would have a single implementation of Interf
's single method.
The fact that the two lambda expressions are defined in the same class doesn't mean that the two implementations of the functional interface's abstract method belong to the same class.
add a comment |
Each of your two lambda expressions implements your Interf
functional interface separately. Therefore each implementation of that interface has a single implementation of that interface's single abstract method.
Even before Java 8 and lambda expressions you could create two anonymous class instances that implement your Interf
interface. Each one of them would have a single implementation of Interf
's single method.
The fact that the two lambda expressions are defined in the same class doesn't mean that the two implementations of the functional interface's abstract method belong to the same class.
add a comment |
Each of your two lambda expressions implements your Interf
functional interface separately. Therefore each implementation of that interface has a single implementation of that interface's single abstract method.
Even before Java 8 and lambda expressions you could create two anonymous class instances that implement your Interf
interface. Each one of them would have a single implementation of Interf
's single method.
The fact that the two lambda expressions are defined in the same class doesn't mean that the two implementations of the functional interface's abstract method belong to the same class.
Each of your two lambda expressions implements your Interf
functional interface separately. Therefore each implementation of that interface has a single implementation of that interface's single abstract method.
Even before Java 8 and lambda expressions you could create two anonymous class instances that implement your Interf
interface. Each one of them would have a single implementation of Interf
's single method.
The fact that the two lambda expressions are defined in the same class doesn't mean that the two implementations of the functional interface's abstract method belong to the same class.
answered Nov 15 '18 at 6:33
EranEran
287k37467557
287k37467557
add a comment |
add a comment |
Actually they are two different implementation of the same interface. In fact, you can use the existing BinaryOperator
to do the same rather than reinventing the wheel like so.
BinaryOperator<Integer> sum = (n1, n2) -> n1 + n2;
BinaryOperator<Integer> substract = (n1, n2) -> n1 - n2;
System.out.println(sum.apply(10, 20));
System.out.println(substract.apply(10, 20));
add a comment |
Actually they are two different implementation of the same interface. In fact, you can use the existing BinaryOperator
to do the same rather than reinventing the wheel like so.
BinaryOperator<Integer> sum = (n1, n2) -> n1 + n2;
BinaryOperator<Integer> substract = (n1, n2) -> n1 - n2;
System.out.println(sum.apply(10, 20));
System.out.println(substract.apply(10, 20));
add a comment |
Actually they are two different implementation of the same interface. In fact, you can use the existing BinaryOperator
to do the same rather than reinventing the wheel like so.
BinaryOperator<Integer> sum = (n1, n2) -> n1 + n2;
BinaryOperator<Integer> substract = (n1, n2) -> n1 - n2;
System.out.println(sum.apply(10, 20));
System.out.println(substract.apply(10, 20));
Actually they are two different implementation of the same interface. In fact, you can use the existing BinaryOperator
to do the same rather than reinventing the wheel like so.
BinaryOperator<Integer> sum = (n1, n2) -> n1 + n2;
BinaryOperator<Integer> substract = (n1, n2) -> n1 - n2;
System.out.println(sum.apply(10, 20));
System.out.println(substract.apply(10, 20));
answered Nov 15 '18 at 6:35
Ravindra RanwalaRavindra Ranwala
10.7k42040
10.7k42040
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313656%2flambda-expression-java-8%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown