Reputation: 2041
Why did new Spliterators
class appear in Java 8? Since Java 8 we have possibility to add static
methods to the interfaces.
Since Spliterators
class has only static method wouldn't be simpler to declare all its methods in the Spliterator
interface
?
The same question about Collectors/Collector
pair.
Thank you.
Upvotes: 4
Views: 131
Reputation: 298133
It’s perfectly possible that this decision was made without even thinking about this brand new possibility, but simply following the established-since-twenty-years pattern.
Besides that, it can be debated whether it is really useful to add 25 to 30 static
methods to an interface. It makes sense to offer a few factories for canonical implementations, but you should draw a line somewhere. It’s not feasible to add factories to all implementations to an interface, just because they are offered by the same library. But this debate would be off-topic.
Further, Spliterators
does not only offer static
methods, but also nested classes. Unlike static
methods, these classes would pollute the name space of every implementation class, when being defined in an interface
.
Collectors
and Spliterators
may also contain implementation-specific non-public
methods and even fields.
Upvotes: 9
Reputation: 1740
No, is not good idea, because interface
declares a contract, but class represents logic. But after add default
method to interface
in Java 8 we can only declare public
method, but in abstract class we can add public
and private
abstract method, so we still can hide some logic in abstract classes. Imagine, in actual level of language you can declare only public
method, and everyone can change your idea for e.q. Collection
Upvotes: 2
Reputation: 29975
Because there is a difference between an interface and a class. These two have different intentions. Interface declares a contract. Default methods for the interface should be used carefully, for instance, where you can't break compatibility by adding a method declaration into an interface and can't declare xxxV2 interface.
A class is an entity, which represents a unit of the program logic.
Upvotes: 0