3 ms·
Mainstream OOP languages (looking at you Java) have failed to make composition as convenient as inheritance.
by crabmusket 2y ago
Mainstream OOP languages (looking at you Java) have failed to make composition as convenient as inheritance.
- flakes 2y agoThe common toolkits today (spring boot, google guice, etc) are much more focused on composition over inheritance, by injecting arguments and implementing pure interfaces rather than extending base classes. Older legacy Java frameworks and bad teachers are more at fault than the Java language itself IMO.
- crabmusket 2y agoI take your point, though having `extends` as a first-class language feature surely encouraged that culture and approach in older frameworks right?
- flakes 2y agoThere are some valid cases where extends really can help, and IMO the language would feel limited without it. Maybe if the language designers had their time back they could have taken an approach like Golang with nested structs and syntactic sugar for calling their attributes/methods. The main reason I see new devs opt for extends, is because that was 99% of the content in their Java 101 programming course, not because it exists in the language. Imagine how many more `friend`s we would have in cpp if that was crammed down everyone's throats? :)
- yxhuvud 2y agoHow is composition inconvenient?
- mdaniel 2y agoContrast the Java way class Delegated implements Base { final Base b; public Delegated(Base b) { this.b = b; } @Override public void printMessage() { b.printMessage(x); } @Override public void printMessageLine() { b.printMessageLine(x); } with the Kotlin way https://kotlinlang.org/docs/delegation.html#overriding-a-member-of-an-interface-implemented-by-delegation https://kotlinlang.org/docs/delegation.html#overriding-a-mem... OT1H, yes, sane people using IJ would just alt-Insert, choose delegate to, and move on with life. But those misguided folks using VS Code, vim, or a magnetized needle and a steady hand would for sure find delegating to a broader interface to be a huge PITA
- _old_dude_ 2y agoVery true, in Java, at least in the last 20 years, inheritance is de-facto deprecated, all new bits and bolts like enums, annotations, lambdas or records do not support inheritance. So you have to use composition.
- watwut 2y agoHow is composition in Java inconvenient?