0

I am new to Java8 and I read a couple of things about this topic on the Internet. For the moment I am trying to figure out what functional interfaces are. I've found some examples, but I do not understand why the interface Skip is a functional one, since it has 2 defined methods. I hope that someone can explain me a bit. The code is:

 @FunctionalInterface
 public interface Sprint 
 {
     public void sprint(Animal animal);
 }


 @FunctionalInterface
 public interface Skip extends Sprint 
 {

      public default int getHopCount() 
      {
         return 10;
      }

      public static void skip(int speed) {}
 }
1

2 Answers 2

5

Your Skip interface has only one abstract method (default and static methods don't count) - the sprint method inherited from the Sprint interface. Therefore it is a functional interface.

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

3 Comments

Oh, I remember that in one post, someone was talking about default methods do not count. But then, someone else said the opposite, so I did not know what to think anymore.. So, do you mean that a functional interface can have as many as one wants, default and static methods?
@Nelly As long as the interface has just one abstract method (i.e. a method that has no implementation), it is a functional interface. The number of default and static methods doesn't matter.
When you're in doubt, read the official documentation: docs.oracle.com/javase/8/docs/api/java/lang/…. It says: Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere. Why do people never read the doc?
2

The best way to think about it is: would it make sense to express an instance of this interface as a single lambda? This is true when there is exactly one abstract method in your interface.

Sprint has the method sprint(), which is abstract. A lambda for this interface would look something like:

Sprint sprint = animal -> {
    animal.doThingOne();
    animal.doThingTwo();
}

Skip has a static method and a default method. static methods aren't anything to do with instances; this is the meaning of static in Java. Additionally, default methods don't have to be implemented in subclasses as a default implementation is already provided. This means that a lambda only has to implement the abstract method in Skip (sprint() again, inherited from Sprint) to be valid. Example:

Skip skip = Animal::doThingThree; // Equivalent to: animal -> animal.doThingThree()

As static and default methods don't have to be implemented by a lambda, you can have as many as you want and still have a functional interface.

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.