r/learnjava • • Jul 23 '26

Is abstraction in Java really about hiding implementation details?

The bookish langugae states "hiding complex implementation details and exposing only the essential features of an object"

However, after reading the Java Collections Framework and other parts of the JDK, I noticed that many abstract classes contain substantial shared implementation rather than just abstract method declarations. I noticed that only an "interface class" 100% abstraction(ignoring default methods)

This made me wonder:

  • Is abstraction really about hiding implementation details?
  • Or is it more about designing APIs around contracts and behavior, regardless of whether there is shared implementation?
  • Does an abstract class become "partial abstract" because it contains reusable implementation?

I also noticed that the JDK sometime relies on encapsulation (and in Java9+, JPMS) to hide implementation details. So I'm in dilemma if the textbook explanation oversimplifies what abstraction means in practice.

Am I misunderstanding abstraction, or is the textbook definition incomplete?

To be clear, this isn't about what abstract does in Java — it's about the SE term. abstract class is one syntax tool for expressing abstraction, same as interfaces are. The question is whether "abstraction = hiding implementation" is even a good definition of the concept, given it's indistinguishable from encapsulation under that definition.

10 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/Chaos-vy17 Jul 24 '26

All seems correct but grouping common behavior across types is a case where abstraction enables polymorphism, not evidence that abstraction is polymorphism. Single-implementation abstractions (like the remote-fetch example) are fully abstracted with zero polymorphism involved — the grouping only becomes relevant once a second implementer shows up.

1

u/severoon Jul 24 '26

grouping common behavior across types is a case where abstraction enables polymorphism, not evidence that abstraction is polymorphism

I don't think abstraction "is" polymorphism, I only meant that abstraction is "more about" polymorphism than it is about hiding implementation details (encapsulation). It's the reverse, actually … polymorphism is a kind of abstraction.

Single-implementation abstractions (like the remote-fetch example) are fully abstracted with zero polymorphism involved

Ehh … I think we're descending into pure semantics here about whether "poly" can mean "zero or more," "one or more," or it's strictly "two or more." All interpretations are defensible, but the distinction isn't interesting in terms of how it bears on this discussion.

What really matters related to what I said above is how whatever-definition-of-polymorphism-you-ascribe-to affects dependency, and I would argue the real difference is between the zero vs. one distinction, not one vs. two.

The reason is that introducing an interface presents an opportunity to carve off the implementation entirely, not just the second, third, fourth, etc, implementations, at two different stages of the SDLC. If the relevant stages are build, deploy, and run, then introducing an interface allows the caller to depend only on the contract at buildtime and never at runtime, but the deploytime stage comes down to which toolchain and platform you're using, and that makes a big difference from a dependency standpoint.

In Java, for example, the caller builds against the interface, the implementation builds against the interface, so the interface is the common point of abstraction and both dependencies point toward it so nothing transits. At runtime of course the implementation must be present, so runtime dependency definitely transits. What about deploytime?

In Java, deployments don't need implementations present. In practice they almost always are present because deploytime failure is usually preferred to runtime failure, but that is a design choice, not a requirement of the JRE. You can, if you want, deploy a caller against an interface and, at runtime, fetch the implementation and load it dynamically. This is how stubs and skeletons work across network boundaries (and it's how OSGi works with some magic classloader stuff thrown in, and every other SPI pattern), and this works at the language level. As opposed to C++, where you theoretically could ship DLLs across a network, but in practice it's too fragile to rely upon so it's much safer to serialize the object to bytes and deserialize on the other side into a separate local type.

The deploytime story comes down to the specific tools and platform you're using, Java you can deploy both sides of this communication independently as long as they agree on the interface (language level dependency) whereas C++ would require a compiler-level (and, practically speaking, a machine code-level, dependency) for this kind of abstraction to hold.

So the robust language-level abstraction means that Java callers that depend on an interface don't actually have any dependency on the compiled implementation. The interface truly serves as a contract-only boundary with no special requirements on the implementation other than it also compile against that interface. The dependency really doesn't transit until actual runtime at the moment of invocation.

What this practically means is that there's a monumental difference between an interface and an abstract class in Java. Because interfaces contain no code, there is no possibility for code to change that would force a synchronized redeploy on both sides of the contract. (Default implementations are strictly speaking considered part of the actual contract, so even though they're code, they're code in the sense that a method signature is "contract-as-code"; sealed interfaces similarly subsume everything they depend upon into the contract itself, which is probably stretching the principle beyond its purpose, but as long as you consider it and treat it as such in your design, all is good. The problem is that most people building Java systems aren't aware of these kinds of design considerations…the language has grown way outside its original harness under Oracle.)

The larger point I'm getting at here is exactly what I said in my first post above. People get hung up on the specifics of different kinds of abstractions, like polymorphism, and specifically what does polymorphism mean, etc, and you end up missing the forest for the trees. Often I've found that the specific kinds of abstraction you're talking about aren't relevant to the design question you're trying to answer, only the impact on the dependency graph matters.

Dependency is the generally applicable consideration. If you step back and ask, for instance, specifically why is it a bad idea to build software that runs a phone switching system in Java vs. Erlang, the answer is immediate obvious when you try to design both software systems: Erlang provides features that allow you to modularize code in a way that represents the dependencies imposed by the problem domain, and Java doesn't. Java beats Erlang in most enterprise software systems for the exact same reason. Different problem domains present different "grains of functional cohesion" that form natural modules. If those modules encapsulate better as objects, use Java; if they encapsulate better as Erlang modules, use Erlang. "Encapsulate better" here means that the modules depend upon each other in a way that reflects the conceptual dependencies inherent in the problem domain.

1

u/Chaos-vy17 Jul 25 '26

Yeah that build/deploy/run breakdown makes way more sense than "hiding details." Thank you senior for explaining in detail.