1

//Base Interface - Functional Interface as it contains only one abstract method

@FunctionalInterface
public interface BaseInterface {

    int sum(int a, int b);

}

Instantiating the functional interface using lambda expression

public class Implementer {

    public static void main(String[] args) {
        BaseInterface base = (num1, num2) -> num1 + num2;

        System.out.println(base.sum(3, 2));
    }

}

Functionality extended in interface - new default method is added

@FunctionalInterface
public interface BaseInterface {

    int sum(int a, int b);

    default String getUserDetails(String name) {
        return name;
    }   
}

If I override the default method to have specific implementation rather than the default one - have to use 'implements' keyword for specifying the interface this way:

public class Implementer implements BaseInterface {

    public String getUserDetails(String uname) {
        uname = uname.trim();
        return uname;
    }

    public static void main(String[] args) {
        BaseInterface base = (num1, num2) -> num1 + num2;

        System.out.println(base.sum(3, 2));
    }

}

In this case, the compiler throws an error and prompts to implement the inherited abstract methods. So, should I have to implement the abstract methods the usual way in this case

public class Implementer implements BaseInterface {

    public String getUserDetails(String uname) {
        uname = uname.trim();
        return uname;
    }

    public static void main(String[] args) {
        //BaseInterface base = (num1, num2) -> num1 + num2;

        System.out.println(base.sum(3, 2));
    }

    @Override
    public int sum(int a, int b) {
        return a + b;
    }

}

(or) is there a way to still use the lambda expression?

5
  • 1
    I think Method References (docs.oracle.com/javase/tutorial/java/javaOO/…) may fulfill your requirement. Usually the reason to use a lambda expression is to pass a function to another function. So you could still have the same code and invoke base::sum(3, 2). If some other method accepts a parameter of type BaseInterface then you could send base::sum as the value to that method. Commented Feb 12, 2018 at 4:06
  • 1
    No. You can't override individual methods using lambda syntax. Commented Feb 12, 2018 at 4:11
  • What happens if Implementer is declared as an abstract class? Commented Feb 12, 2018 at 4:53
  • @FedericoPeraltaSchaffner Making the class as abstract does not prompt. I'm aware that abstract class can absolutely have non-abstract methods, but is it a good practice without having non-abstract methods but still declaring it abstract? Commented Feb 12, 2018 at 5:33
  • By keeping the Implementer class abstract you don't implement the sum method, that's why the class is still abstract. Anyway, it's absolutely OK to have an abstract class without abstract methods. An abstract class represents something you cannot instantiate, but it doesn't need to contain abstract methods. Commented Feb 12, 2018 at 14:35

1 Answer 1

2

You can override the default method in another interface that extends your BaseInterface:

public interface Extender extends BaseInterface {
    @Override
    default String getUserDetails(String uname) {
        uname = uname.trim();
        return uname;
    }
}

and then Extender e = (a, b) -> 2 * a + 2 * b;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.