5 ms·
I rarely see inheritance used in practice in java code bases. Except where I would use a union type or sealed class in other languages anyways. I don't feel wha
by matsemann 3y ago
I rarely see inheritance used in practice in java code bases. Except where I would use a union type or sealed class in other languages anyways. I don't feel what youre describing is a real issue.
- xxs 3y agoInheritance used to be extremely common, look at AWT/Swing - however more or less it finished there, e.g. more than 20y back. There are still lots of folks who love 'protected' and deep hierarchies, of course. There is stuff like spring that uses way too many interfaces with a single implementation, doing something a bit off the regular road ends up implementing tons of the said interfaces anew. However the 'hate' part is mostly the internet (well esp. Hacker news)warrior topic
- sisve 3y agoGreat to hear. I used to program java in 2010 and inheritance was still beeing heavily used back then. But things change, if both the lang and the main community has change to focus more on simplicity its def worth looking at again. Coroutines and pattern matching is really good features
- insanitybit 3y agoContrast that, I've seen numerous Java codebases, young and old, and inheritance is very much one of the core ways that people program. I strongly suspect that in a few cases some Java devs using net new systems and avoiding common frameworks will perhaps be able to avoid lots of inheritance but I find it insane to say that that's common or even easy.
- School-Cotton 3y agoInheritance is heavily used in Java in 2023, at least in projects I’ve had to look at.
- sisve 3y agoSorry, what? Is not java toghether with Ruby maybe the most hardcore OOP enthusiasts that uses inheritance for "most stuff". Has the java culture move so far the last decade ?
- fiddlerwoaroof 3y agoI’ve been writing OOP code and reading about it for a decade or two, and inheritance was identified as problematic pretty quickly. The problem today, imo, is more underuse now.
- fuzztester 3y ago>inheritance was identified as problematic pretty quickly Exactly what I referred to here: https://news.ycombinator.com/item?id=37539443 https://news.ycombinator.com/item?id=37539443
- fiddlerwoaroof 3y agoYeah, it’s a bit like how the school version of lisp makes people think real lisp code bases are all lists and recursion.
- dragonwriter 3y agoEven in Ruby, IME, deep inheritance is fairly uncommon. Wide inheritance through mixins is common, though.
- EdwardDiego 3y agoEffective Java, written by Joshua Bloch, and first published in 2001, had a very influential section called Favor composition over inheritance: https://books.google.co.nz/books?id=Ra9QAAAAMAAJ&q=inheritance&source=gbs_word_cloud_r&cad=4 https://books.google.co.nz/books?id=Ra9QAAAAMAAJ&q=inheritan... Why do you think DI is so prevalent in Java codebases? It's a great way of simplifying composition.
- 3y ago
- insanitybit 3y ago> I rarely see inheritance used in practice in java code bases. That seems absurd to me and I have a hard time understanding it, honestly.
- wayfinder 3y agoIf you type everything with interfaces in your codebase, you are much less tied to inheritance. In fact, everyone could be written to be composed. However Java doesn’t support type union so you can get into some ugly and verbose situations but the JVM doesn’t really check type so this is more a compile-time issue and could be fixed in a future Java language revision.
- logicchains 3y ago>However Java doesn’t support type union so you can get into some ugly and verbose situations Isn't the "sealed interface" described in the OP blog post a type union? Or you mean anonymous unions?
- wayfinder 3y agoDefinitely referring to anonymous unions too. Without them, there’s still friction sometimes which makes union types unnatural. I haven’t written Java in a while and I can’t remember if you could sometimes fake a type union using a generic type on a method, but if you can, it’s definitely super ugly and would raise eyebrows during any code review.
- logicchains 3y agoAnonymous unions are a relatively rare feature; Rust and Haskell don't have them, for instance.
- Supermancho 3y ago> I rarely see inheritance used in practice in java code bases. Without some specific call-out, it can be assumed that this is a very niche viewpoint that has no bearing on modern development. The vast majority of projects use inheritance, today. Does it use Spring? extends SpringBootServletInitializer Meaningful responses using values from a request? extends OncePerRequestFilter Formatting exception handling responses on web requests? extends ResponseEntityExceptionHandler Then there's all the interfaces you have to satisfy, because it's all tied into the standard library and popular libraries.
- tail_exchange 3y agoYou must be really lucky then. In my previous job, inheritance and abstract classes were everywhere. Coupled with dependency injection frameworks that "worked like magic", it was really hard to follow the code inside that big, monolithic app. It made me never want to work with Java ever again.
- bb88 3y agoI've never been a big fan of dependency injection. It solves a problem with unit testing in Java, sure, but the reality is that Java could have done some nifty things to help alleviate that as well.
- jhaenchen 3y agoAgreed. Java injection frameworks are opaque and accomplish what they need to with overly powerful mechanisms because of the nature of the language. You don't see that sort of nonsense in python.
- kaba0 3y agoWtf, so what do you think Django is?
- bb88 3y agoI'm curious to understand what dependency injection is in Django that you're referring to? I know pytest does DI with fixtures, and trying to figure out what you're pulling into a test can be difficult.
- matsemann 3y agoIn django you're importing a default cache, a default storage etc, and write your code to the interface. In settings.py you wire it all up. It's basically the same, for testing you would have to mock the import or provide some implementation.
- rerdavies 3y agoHtmlElement. AST. Socket. Stream. Window. Closable. Runnable.
- fuzztester 3y agoYour Point? Long: Please make your Point Understandable.
- rerdavies 3y agoExamples of classes where inheritance is indispensable. I cannot imagine what kind of programming you are doing where that pattern never comes up.