Reason for adding default and static methods in in

2020-02-08 18:36发布

Java 8 introduced default and static methods on interfaces. So now you can have concrete implementations in your interface whether using default or static methods.

The reason Java claimed to add these two new kind of methods is "ensure binary compatibility with code written for older versions of those interfaces".

My question:

  • Why to distort the interface original concept that suppose to be fully abstract in order to support existing architectural problems?
  • What is the difference between using an abstract class and the new version of the interface other than the ability of a class to extend multiple interfaces?

2条回答
Ridiculous、
2楼-- · 2020-02-08 19:29

The reason java claimed to add these 2 new kind of methods is "ensure binary compatibility with code written for older versions of those interfaces".

This applies only to default methods (not static methods) and omits some context. From Goetz, State of the Lambda:

The purpose of default methods ... is to enable interfaces to be evolved in a compatible manner after their initial publication.

The main goal is to allow interface evolution, that is, the addition of new methods. If a new method is added to an interface, existing classes that implement the interface would be missing an implementation, which would be incompatible. To be compatible, an implementation has to come from somewhere, so it is provided by default methods.

Why to distort the interface original concept that suppose to be fully abstract in order to support existing architectural problems?

The main intent of a Java interface is to specify a contract that any class can implement without having to alter its position in the class hierarchy. It's true that, prior to Java 8, interfaces were purely abstract. However, this is not an essential property of interfaces. Even when default methods are included, an interface at its heart still specifies a contract upon the implementing class. The implementing class can override default methods, so the class is still in complete control of its implementation. (Note also that default methods cannot be final.)

What is the difference between using an abstract class and the new version of the interface other than the ability of a class to extend multiple interfaces?

The ability of a class to extend multiple interfaces is closely related to another difference between interfaces and abstract classes, namely that interfaces cannot contain state. This is the primary difficulty with allowing multiple inheritance: if a superclass were to appear multiple times in the ancestry of a class, would that superclass' state appear just once or several times? (This is the so-called "diamond problem.")

Another difference is that abstract classes can define methods and fields to be shared with subclasses, but not with callers, by using protected and package-private access levels. Interfaces can have only public methods.

(In Java 9, support for private methods has been added. This is useful for implementation sharing among default or static methods of an interface.)

Finally, static methods in interfaces don't affect class inheritance, nor are they part of the interface's contract. They are merely a way of organizing utility methods in more convenient fashion. For example, a common use of static methods in an interface is for static factory methods. If static methods weren't allowed in interfaces, static factory methods would have to be put on a companion class. Allowing static methods in interfaces lets such methods be grouped with the interface itself, when doing so is appropriate.

查看更多
smile是对你的礼貌
3楼-- · 2020-02-08 19:36

The problem is, that you can never extend an Interface with a new method without breaking compatibility. Existing classes would not implement the method and therefore not run with the new version of code which use this method.

This was a major problem for the Java Class Library itself as it cannot add often asked for methods in basic interfaces (like Collections). This was the main driver for implementing default methods for interfaces.

The difference between this new method and using an abstract class (which is a quite good pattern for this problem in some cases) is, that you cannot inherit from multiple abstract classes. But you can easily implement multiple interfaces.

The static methods in interfaces are less clear, I think they are here to help you implement the default methods (if two default methods have same code, they both can call to a static method).

查看更多
登录 后发表回答