2

Why We need Defender methods in interfaces in java 8 as we already have abstract classes.I found various answers on internet like:

To add external functionality

But Abstract class is for partial Abstraction where as our interface is actually a pure abstract class so why their is a default method inside an interface?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Gurinder
  • 943
  • 2
  • 9
  • 19
  • 1
    @Siguza The two are sort of synonymous, while "default" is the official one. – Sergey Kalinichenko Sep 04 '15 at 19:15
  • The usual answer (which is easy to find) is that they needed to add methods to interfaces like `Collection`; without default methods that would break massive amounts of third party classes that would implement `collection` but not the new methods. – SJuan76 Sep 04 '15 at 19:17
  • @VGR not a dupe but one answer explains it well enough – SJuan76 Sep 04 '15 at 19:18

3 Answers3

4

The problem with sharing functionality by placing it in an abstract base class is that a class can derive from exactly one base class. This is a limitation in cases when you would like to inherit functionality from more than one base.

Sharing functionality through an abstract base class may also become a problem when you need to implement an interface from a class that already has a base class. In this case you cannot derive your new class at all, because you must pick one of the two bases, when presumably you want both.

Default methods solve this problem with elegance: placing your common implementation into the default method allows you to share the code without limitations.

You can think of the main difference between default methods and inheriting an abstract class as the difference between sharing functionality horizontally across siblings that implement the same interface, or vertically among children that inherit from the same base class.

Here is an examoke: consider an interface that looks like ResultSet of JDBC, which has two ways of accessing the same column - by name and by index. The interface could be coded up like this:

interface ResultSet2 {
    int findColumn(String columnLabel);
    String getString(int index);
    long getLong(int index);
    default long getLong(String columnLabel) {
        return getLong(findColumn(columnLabel));
    }
    default String getString(String columnLabel) {
        return getString(findColumn(columnLabel));
    }
}

Anyone implementing ResultSet2 would have to implement three methods, and get the remaining two for free. They would have an option to provide an alternative implementation, but that would be optional.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The main reason behind defender methods is to be able to extend long-existing interfaces with new functionality, without breaking the existing code. Particulary with Java 8 lamba expressions they introduced a lot of new methods on collection interfaces, like Iterable.forEach. With providing default methods, existing classes implementing the Iterable interface dont have to be altered to use in Java 8 environment.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • You are wrong regarding "without breaking the existing code". If you add to the interface a method that is already defined in some implementation classes and is not compatible with them, for instance it has a different result type, you WILL BREAK the existing code. Guys have you heard of open/closed principle? Brian Goetz and others in Oracle have definitely didn't hear about it when they designed such a thick language feature. – mentallurg Dec 27 '17 at 08:30
1

The original intent was to compete with C#'s extension methods. Given core methods of an interface, e.g. get(), set() in List, extention methods (e.g. sort()) can be defined and implemented.

Java guys argued that it would be better to declare such methods on the interface itself, rather than in external places; so that the methods could be overridden by subtypes, providing optimal implementations per subtype. (They also argued that such methods should be controlled by the interface authors; this is rather a soft point)

While default methods can be added to existing interfaces, it is very risky of breaking existing 3rd party subtypes, particularly for very old types like List with lots of subtypes in the wild. Therefore very few default methods were added to existing core Java APIs. See this question.

For new interfaces, default method is a very valuable tool for API designers. You can add a lot of convenience methods to an interface, for example, Function.compose(). Subtypes only need to implement abstract/core methods, not default methods (but they can if they want to).

I disagree with the idea that default methods can "evolve" interfaces. They do not change the core semantics of an interface, they are just convenience methods (in the form of instance method).

And default methods should be carefully designed up-front when the interface is designed; as said, it is very risky to add default methods afterwards.


C#'s extension method allows 3rd party to add convenience methods; this is very nice, and there is no reason why Java couldn't introduce something similar in future.

Community
  • 1
  • 1
ZhongYu
  • 19,446
  • 5
  • 33
  • 61