Designing interfaces have always been a tough job because if we want to add additional methods in the interfaces, it will require change in all the implementing classes.
But the default interface methods and static interface methods in Java 8 accomplish to solve this pain.
Defualt Method
For creating a default method in java interface, we need to use “default” keyword with the method signature. For example,
Notice that log(String str) is the default method in the I1. Now when a class will implement I1, it is not mandatory to provide implementation for default methods of interface. This feature will help us in extending interfaces with additional methods, all we need is to provide a default implementation.
Let’s say we have another interface with following methods:
We know that Java doesn’t allow us to extend multiple classes because it will result in the “Diamond Problem” where compiler can’t decide which superclass method to use. With the default methods, the diamond problem would arise for interfaces too. Because if a class is implementing both I1 and I2 and doesn’t implement the common default method, compiler can’t decide which one to chose.
So to make sure, this problem won’t occur in interfaces, it’s made mandatory to provide implementation for common default methods of interfaces. So if a class is implementing both the above interfaces, it will have to provide implementation for log() method otherwise compiler will throw compile time error. But we also could use the I1’s default method in the following example,
Tips of Default Method
- Java interface default methods will help us in extending interfaces without having the fear of breaking implementation classes.
- Java interface default methods has bridge down the differences between interfaces and abstract classes.
- Java 8 interface default methods will help us in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
- If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from
java.lang.Object
. The reasoning is very simple, it’s becauseObject
is the base class for all the java classes. So even if we haveObject
class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class methods. - There could be many default methods in an interface
Static Method
Java interface static method is similar to default method, but we cannot override them in the implementation classes(there could be a method with same name). This feature helps us in avoiding undesired results in case of poor implementation in sub classes. Let’s look into this with a simple example,
Notice that Java interface static method is visible to interface methods only, the sub classes cannot use the static interface methods. However like other static methods, we can use interface static methods using interface name.
Tips of Static Method
- Java interface static method is part of interface, we cannot use it for implementation class objects.
- Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
- Java interface static method helps us in providing security by not allowing implementation classes to override them.
- We cannot define interface static method for
Object
class methods, we will get compiler error as “This static method cannot hide the instance method from Object”. This is because it’s not allowed in java, sinceObject
is the base class for all the classes and we cannot have one class level static method and another instance method with same signature. - An interface could have both many default methods and static methods at the same time.
Functional Interface
One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
An interface with exactly one abstract method is known as Functional Interface.A new annotation @FunctionalInterface has been introduced to mark an interface as Functional Interface. @FunctionalInterface annotation is a facility to avoid accidental addition of abstract methods in the functional interfaces. It’s optional but good practice to use it.
@Functional interfaces are long awaited and much sought out feature of Java 8 because it enables us to use lambda expressions to instantiate them. A new package java.util.function with bunch of functional interfaces are added to provide target types for lambda expressions and method references.
I will look into functional interfaces and lambda expressions in the future posts.