My question is there is a method stream in collection in java 8 which has return type stream which is an interface and there is no class that implements this interface so how this method returns object of stream interface? because we can not create object of interface in java. here is stream method screenshot which is in collection interface
Asked
Active
Viewed 58 times
-1
-
“_and there is no class that implements this interface_” - how did you draw this conclusion? There are several. Why not take a look at [the implementation](https://github.com/openjdk/jdk/blob/534f00510e26da8deb891b4184c860ec64991b8f/src/java.base/share/classes/java/util/Collection.java#L742)? – Boris the Spider Jul 20 '21 at 05:20
-
It returns an implementation of `Stream`. See [What does it mean to "program to an interface"?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). The `Collection#stream()` method is also a default method so implementations of `Collection` don't necessarily have to override said method. – Slaw Jul 20 '21 at 05:42
-
I got it now thanks @Slaw – Mehul Jul 20 '21 at 06:35
-
"and there is no class that implements this interface" I conclude this because if I look at implementation of stream interface in official java 8 docs there is not mentioned any subclass of this stream interface so @BoristheSpider – Mehul Jul 20 '21 at 06:45
-
2That's because the implementation is not public API. See https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/stream/ReferencePipeline.java – Slaw Jul 20 '21 at 06:52