Introduction
All right, I failed another interview as usual. I always made very simple errors, like yesterday, I had an interv iew with an india company. Their interview questions are more focused on basic knowledge of candidate. I record servral questions which I failed to answer. Here I reviewed them and write the answers out.
1. Functional Interface
The first question is about one the feature of Java 8, functional interface. To be honest, I just have a very ambiguous overview about it.
I write down the answer below.
1.1 Introduction
Functional interface is defined by only have single abstract method, so it is also called SAM interface, but it accepts multiple default and static methods.**
@FunctionalInterface
public interface MyInterface {
public int add(int a, int b);
}
//Usage
public class App {
public static void main(String[] args) throws Exception {
MyInterface myInterface=(x,y)->x+y;
}
}
You might notice that there is no abstract keyword used before add() method, that is because, by default, a method declared inside a functional interface is abstract.
1.2 Declaration
You might have noticed that the first line of above snippet is @FunctionalInterface annotation. Yes, it’s built-in Java annotation. You must have it to build your own functional interface.
Apart from that, Java also offers some predefined functional interfaces, like Runnable, Callable, Function, Consumer etc.
1.3 Benefits
The main reason why we need functional interfaces is that we can use them in lambda expressions and method references. This way, we reduce boilerplate code.
Before, we would have to use an Anonymous class, like below. But functional interfaces make the code more straightforward to read. For example, an anonymous class is created like this:
public class AnonymousClassExample {
public static void main(String[] args) {
Thread thread = new Thread(){
public void run(){
System.out.println("Understanding what is an anonymous class");
}
};
thread.start();
}
}
Now, with Runnable interface, it looks like this.
public class AnonymousClassExample {
public static void main(String[] args) {
Runnable runnable = () -> {
System.out.println("Understanding functional interfaces");
};
runnable.run();
}
}
It’s shorter and easier to read, right?
2. Default method
Another mistake I made is mixed Static method with another feature of Java 8, Default method.
Before Java 8, interface can only contain abstract method. Since Java 8, Static method and Default method are introduced in interface. It hugely expands the functions of interface without breaking the heritance relationship of its derived class.
There’s an interface
public interface MyInterface {
public int add(int a, int b);
}
User asks to add another mothod into MyInterface, like divid(int, int). With Static method and default method, I can accomplish it without defining a divid(int, int) abstract method.
- The example of Static method
@FunctionalInterface
public interface MyInterface {
public int add(int a, int b);
static double divid(int a, int b){
System.out.println("I am static");
}
}
//Usage
public class App {
public static void main(String[] args) throws Exception {
System.out.println(MyInterface.divid(3, 3));
}
}
//result
PS D:\code\FuntionalInterface> d:; cd 'd:\code\FuntionalInterface'; & 'C:\Program Files\Java\jdk-17.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'D:\code\FuntionalInterface\bin' 'App'
1
- The example of Default method
@FunctionalInterface
public interface MyInterface {
public String des="abc";
public int add(int a, int b);
default public int divid(int a, int b){
return a*b;
}
}
//Usage
public class App {
public static void main(String[] args) throws Exception {
MyInterface myInterface=(x,y)->x+y;
System.out.println(myInterface.add(1, 1));
}
}
//result
PS D:\code\FuntionalInterface> d:; cd 'd:\code\FuntionalInterface'; & 'C:\Program Files\Java\jdk-17.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'D:\code\FuntionalInterface\bin' 'App'
2
- Difference
Default methods:
A default method is a method declared with the default keyword in a functional interface.
Default methods have a default implementation, which can be overridden by implementations of the functional interface in concrete classes.
Default methods are used to provide a default behavior for the functional interface, but can be customized by concrete classes if needed.
Static methods:
A static method is a method declared with the static keyword in a functional interface.
Static methods are not associated with a particular instance of the functional interface and are called using the name of the functional interface, not an instance of the interface.
Static methods are used to provide utility methods that are associated with the functional interface as a whole, rather than with individual instances of the interface.