Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason for adding default and static methods in interfaces

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?
like image 251
Hussein Zawawi Avatar asked May 02 '15 00:05

Hussein Zawawi


1 Answers

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.

like image 139
Stuart Marks Avatar answered Oct 19 '22 04:10

Stuart Marks