Lokesh Baranwal
Lokesh Baranwal

Reputation: 33

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

Upvotes: 3

Views: 2884

Answers (3)

Atul Jain
Atul Jain

Reputation: 1085

Let me tell you the proper use case.

    class Person {
      String firstName;
      int age;
      enum Gender {
         MALE, FEMALE;
       }

      Double salary;

      Gender gender;

      public Person(String firstName, int age, Double salary, Gender gender) {
        super();
        this.firstName = firstName;
        this.age = age;
        this.salary = salary;
        this.gender = gender;
      }

    void printPerson(Person p, PersonFilter pf) {
        if (pf.filter(p)) {
            System.out.println("First Name: "+p.firstName+" Age: "+p.age+" Salary: "+p.salary+" Gender: "+p.gender);
        }

    }
}

interface PersonFilter {  // Functional interface
    boolean filter(Person person);
}
public class LambdaExpressionTwo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Atul", 31, 10000.0, Person.Gender.MALE));
        persons.add(new Person("Mohan", 32, 17500.0, Person.Gender.MALE));
        persons.add(new Person("Mic", 34, 15000.0, Person.Gender.FEMALE));
        persons.add(new Person("Ricky", 30, 15000.0, Person.Gender.MALE));

        /*persons.forEach(p-> {  // Search Criteria Code in an Anonymous Class
            p.printPerson(p, new PersonFilter() {
                @Override
                public boolean filter(Person person) {
                    return p.gender == Person.Gender.FEMALE;
                }
            });
        });*/
        
        persons.forEach(p-> { // Search Criteria Code with a Lambda Expression
            p.printPerson(p, (Person p1) -> p1.gender == Person.Gender.MALE);
        });
    }
}

Less coding with Lambda Expression.

Upvotes: 0

Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

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)); 

Upvotes: 3

Eran
Eran

Reputation: 393771

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.

Upvotes: 3

Related Questions