How can a close() method invoked from Stream point to the implementation of the close() method in the AbstractPipeline abstract class?
Asked Answered
H

1

6

I just started learning Java Streams, and I have a question. Something that confuses me a lot is the following:

  1. I just checked out the AutoCloseable interface and it holds the close() method.

  2. The BaseStream interface extends the AutoCloseable interface, and the rule of inheritance supplies, meaning that the close() method can be used from the BaseStream interface.

  3. The Stream interface extends the BaseStream interface, and the rule of inheritance supplies, meaning that the close() method can be used from the Stream interface.

  4. Also, the abstract class AbstractPipeline implements the BaseStream interface and it provides an implementation of the close() method.

This is the diagram of the previously described.

List<String> stringList = new ArrayList<>();
Stream<String> stringStream = stringList.stream();
stringStream.close();

When I hold the Ctrl keyboard button and click left click on the mouse it points me to the abstraction of the close() method in the BaseStream interface, but when I press Ctrl+Alt+B it points me to the implementation of the method in the AbstractPipeline abstract class.

The question is:

How can a close() method that is invoked of Stream point me to the implementation of the close() method in the AbstractPipeline abstract class, when Stream and AbstractPipeline do not interact and their main connection is the BaseStream?

Is this some kind of object oriented concept that i'm not aware of?

Thank you.

Update:

I'm using IntelliJ IDEA 2020.1.2 (Ultimate edition) and JDK 1.8 but I've also tried it with JDK 14 and Eclipse IDE for Enterprise Java Developers version 2019-06 (4.12.0) and it is the same.

Hekker answered 2/7, 2020 at 15:29 Comment(6)
Maybe IntelliJ knows for some reason that the actual implementation of your call to close() will be that of AbstractPipeline, but however, this is either an feature or bug or IntelliJ (or whatever IDE you use) and has nothing to do with Java or any OOP ConceptLoquacity
You should mention in your Question what IDE you are using, since that is partly the core of your Question.Renzo
I'm using IntelliJ IDEA 2020.1.2 (Ultimate edition) and JDK 1.8 but I've also tried it with JDK 14 and Eclipse IDE for Enterprise Java Developers version 2019-06 (4.12.0) and it is the same.Hekker
What happens when you debug that code and step into the close() method; where do you land?Calia
If I put a brake-point in the close() method of the AbstractPipeline abstract class it goes there, but that is not the problem. My question is why does it go there? I have attached a diagram which shows something that is not logical to me, please check out the diagram. Am I not seeing something?Hekker
There is a class X extending AbstractPipeline, implementing Stream, but not overriding the inherited close() method. Perhaps, there is more than one class with these properties, but the debugger should show you which actual class you encountered when stepping into the invocation of the close() method. I’m not sure, what problem you have with this concept. Your IDE is capable of jumping to this particular implementation method even at compile-time, because it has scanned the environment and found that all implementation classes have these properties, so that close() is the only one.Calia
H
1

There is an abstract class ReferencePipeline which extends abstract class AbstractPipeline and implements Stream, but isn't overriding the close() method. This abstract class is the connection and the answer I was looking for. Now, everything seams reasonable and is clear for me. Here is the full diagram, which clears the answer to this question.

enter image description here

Thanks to everyone that participated and helped me find the answer. Cheers!

Hekker answered 7/7, 2020 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.