Java 8 Interfaces

Posted on Posted in Architect, Java

There are several changes for interfaces, including the addition of static and default methods. Interfaces in java 8 is more and more similar to abstract method.  Consider the example below:

Key notes:


  • static methods inside interfaces are only accessible inside the interface.
  • No way to access from outside the interface, see initializeTestData() method above.


  • default are concrete methods for interfaces, that can be or not overwritten
  • this enables you to give default behaviour of interfaces without having to implement them, for example in this case, testable has a default behaviour of printing out some text.
  • you can’t have multiple interfaces with same method that is implemented by one class see the below:
  • both Testable, Untestable have the same method test(), resulting in a compiler error

the code above will require you to provide a test method in your class.

Lambda and Functional Interfaces

There is a new of interaction for lambda and interfaces. First of, what are functional interfaces? These are interfaces that only have one sole method. A great example of this is the interface Runnable, when you want to run threads. Another interface well known is Comparablefor when you want to compare objects.

Now with lambda you can instantiate functional interfaces instantly, consider the below:

Key Notes:

  • Notice the syntax for Runnable, whatever we pass inside the curly brackets, will automatically be the implementation of the single method in the interface.
  • You can create your own functional interface, and have parameters as well, see syntax for Bounceable
  • Functional interfaces cannot have more than one method, otherwise lambda expressions will not know which method to use
  • There is a new package in java that is solely containing functional interfaces for lambda expressions, will look into that in the next post.


Leave a Reply