Up until now, interfaces in Java couldn't implement any method. By definition, they were only a collection of method signatures with no body.
But it seems this changed with Java 8.
Is this true? What are 'default methods' exactly? What is a 'functional interface'? Anybody care explain this to me? Things on Google are pretty confusing.
In Java 8, Interfaces
can now have default
methods which are actually implemented. This was done to help avoid problems for users who implement an Interface
that has had a change made to it, so they don't have to update all of their classes.
You add this functionality by adding the default
keyword to your method signature.
See The Java Tutorials
What are 'default methods' exactly?
Default methods provide a mechanism for you to add implementation to an interface without making it an abstract class.
The main use case for default methods has been the task of adding methods to an interface without breaking other people's code. However, they also let you "mix in" functionality through implementing an interface, which is a very powerful addition to Java's type system that used to allow only a single line of implementations.
What is a 'functional interface'?
Functional interface, on the other hand, is a way to tell the compiler that your interface is going to have exactly one method. In exchange for that the compiler gives you a much shorter syntax for defining implementations of your interface, dramatically shortening the code that used to depend on anonymous classes.
In addition to the two features above, interfaces let you define static functions to be shared among all implementations. Like default implementations, this adds a "second dimension" to inheriting implementation, because it lets you share code through interface implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With