8 ms·
> We now know that we prefer composition over inheritance When people say "composition over inheritance" in Java discussions, they usually mean the trivial mod
by owlstuffing 6mo ago
> We now know that we prefer composition over inheritance
When people say "composition over inheritance" in Java discussions, they usually mean the trivial modeling rule: prefer has-a over is-a.
But that’s not what composition is really about.
The deeper idea is interface composition -- building types by composing multiple behavioral contracts behind a single cohesive surface.
Java provides inheritance and interfaces, but it doesn’t provide first-class delegation or traits. So most developers never really practice interface composition. They either subclass, or they wire objects together and expose the wiring.
The slogan survived. The concept mostly didn’t.
The manifold project, for example, experiments with language-level delegation to make interface composition practical with Java.
https://github.com/manifold-systems/manifold/blob/master/manifold-deps-parent/manifold-delegation/README.md https://github.com/manifold-systems/manifold/blob/master/man...
- gf000 6mo agoWell, what mainstream language has better tools for composition?
- fingerlocks 6mo agoRust with traits and Swift with protocols
- gf000 6mo agoSo just type classes? How does a type class help with composition? They do help with the expression problem (adding support for an "interface" after definition), and via parametric polymorphism they might give you a bit with regards to composing two traits.. but you do also have generics in Java, even if not as good as type classes. So anyways, I don't see as big of a change here. But there was a Brian Goetz mail/presentation somewhere where he talked about adding "basically type classes" to Java? But unfortunately I couldn't find it for you now.
- joe_mwangi 6mo agoHere it is https://youtu.be/Gz7Or9C0TpM?si=Rk5pTeAC6ibqS-M- https://youtu.be/Gz7Or9C0TpM?si=Rk5pTeAC6ibqS-M-
- megapoliss 6mo agokotlin
- owlstuffing 6mo agoKotlin's "delegation" feature isn't true delegation, it's just call forwarding, which is better than nothing, but it falls down pretty quickly as an alternative to implementation inheritance. The manifold project provides true delegation[1] for Java. 1. https://github.com/manifold-systems/manifold/blob/master/manifold-deps-parent/manifold-delegation/README.md https://github.com/manifold-systems/manifold/blob/master/man...
- haspok 6mo ago> Java provides inheritance and interfaces, but it doesn’t provide first-class delegation or traits. I'm not sure I am missing first class delegation much (not a lot of UI projects in Java these days). But interfaces with default (and static) method implementations are actually quite usable as traits / mixins. Since Java 8 IIRC. You can also pass around functions / lambdas (coincidentally also since Java 8) to compose functionality together. A bit harder to follow and/or understand, but another perfectly legitimate and very powerful tool nevertheless.