Default and Static Method of JDK8 Interface – Detailed Explanation of the Difference between Interface and Abstract Class

Time:2019-9-10

Introduce

After JDK 1.8, the interface allows the definition of default methods and static methods, such as foreach methods in the Iterable class.


public interface Iterable<T> {
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
Iterator<T> iterator();
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
...
}

You can see that interfaces are becoming more and more similar to abstract classes.

Design intention

In the past, if we want to add methods such as foreach to the interface, its subclasses (such as collection classes) must implement this method all. This is a bit unrealistic. After adding default method, we solved this problem. The interface used to be an abstraction of behavior, a pure design model, and now the interface also undertakes code refactoring. There are advantages and disadvantages to some responsibilities..

Method conflict

A class can implement multiple interfaces, that is to say, default method conflicts may occur in the following cases:

1. If the interface inherits from another interface, the default method of the parent interface is retained if both interfaces have the same default method.

2. If a class implements two interfaces, one is the default method, and the other is the default method or the abstract method, the class needs to override the method.

3. If the default method in the interface conflicts with the method in the inherited parent class, the method in the parent class is preferred, which is compatible with the previous version, and the noun is called the class priority principle.

Interface static method
Static method localization in the interface is the tool method, which is called directly by the interface name. For example: Comparator interface


/**
* Accepts a function that extracts a sort key from a type {@code T}, and
* returns a {@code Comparator<T>} that compares by that sort key using
* the specified {@link Comparator}.
*
* <p>The returned comparator is serializable if the specified function
* and comparator are both serializable.
*
* @apiNote
* For example, to obtain a {@code Comparator} that compares {@code
* Person} objects by their last name ignoring case differences,
*
* <pre>{@code
* Comparator<Person> cmp = Comparator.comparing(
* Person::getLastName,
* String.CASE_INSENSITIVE_ORDER);
* }</pre>
*
* @param <T> the type of element to be compared
* @param <U> the type of the sort key
* @param keyExtractor the function used to extract the sort key
* @param keyComparator the {@code Comparator} used to compare the sort key
* @return a comparator that compares by an extracted key using the
* specified {@code Comparator}
* @throws NullPointerException if either argument is null
* @since 1.8
*/
public static <T, U> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator)
{
Objects.requireNonNull(keyExtractor);
Objects.requireNonNull(keyComparator);
return (Comparator<T> & Serializable)
(c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
keyExtractor.apply(c2));
}

The Difference between JDK8 Abstract Class and Interface

Similarities:

1. They are abstract types.

2. All methods can be realized.

3. We can implement the method without relying on the implementer or the successor.

Difference:

1. The abstract class can not inherit more, and the interface can.

2. The design concept of abstract class and interface is different. The abstract class is is-a, the interface is like-a, the abstract class is the abstraction of class, and the interface is the abstraction of behavior.

3. Membership method access is different, abstract class allows non-final attributes, public, private and protected methods, but interface only allows constant attributes, methods are public.

model selection

If you have access rights to relational properties and methods, consider abstract classes. If you focus on multiple inheritance, consider interfaces.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.