3 ms·
Please ELI5 what is wrong with inheritance and/or how Java have it wrong. Do we need to go back to a new object oriented undegraduate course? Genuinely asking.
by wslh 3y ago
Please ELI5 what is wrong with inheritance and/or how Java have it wrong. Do we need to go back to a new object oriented undegraduate course? Genuinely asking.
- insanitybit 3y agoInheritance provides "is a" relationships between classes. At a time when people would spend months designing their software upfront, building big diagrams of classes, etc, this was not so bad. You'd have a very clean system design that maps directly to your class design. The problem is when things change. A simple example - you build a classification of all life and it is built upon the idea that everything "is a" plant or animal. And then one day it turns out there are fungi. This is not a fun situation to deal with and inheritance makes it a lot harder because the "is a" relationship is driving a ton of your logic. IDK I don't want to get to into it beyond that, many people have written quite a lot on the topic.
- wayfinder 3y agoThis problem is partly a type system limitation however. In a more flexible type system with union types and other magical features, your example problem would be less of an issue. However Java has an extremely limited type system so there is no middle ground between composition and inheritance. Once you choose one way, there is no “middle step” to migrate over.
- kaba0 3y agoUnion types are very rare (scala, typescript are the notable ones I can think of that implement it), most other languages only have sum types (which includes java as well, see sealed interface/classes), where you have to create each unique set of types you want to use separately, wrapping each option into a marker type basically.
- kaba0 3y ago> you build a classification of all life and it is built upon the idea that everything "is a" plant or animal With all due respect, this and similar examples are just plain wrong, and I really can't take anyone seriously when that example is used. The point of programming, and its abstractions is to help you complete a task, and make the implementation maintainable and easy to reason about. I think grabbing the "is a" part is fundamentally bad -- there is no point in creating a taxonomy in and of itself, this is no database for that data. Inheritance sometimes is the correct abstraction and while it is definitely overused, when it's correctly applied there isn't really another abstraction that would fit better. E.g. see a graphics library's Node class as the stereotypical correct application.
- insanitybit 3y agoI think your post can be broken down into two main points. 1. That my point is bad for some reason 2. That inheritance is sometimes a great tool We agree on (2). Inheritance is pretty amazing, even if I think that it's ultimately a terrible feature to build so ingrained into a language and to expand in power to such a degree. As for (1), I don't really get your point. Abstractions help you complete a task - ok. Abstractions are to help you reason about stuff - ok. Something about "is a" being bad? None of that really explains why my example demonstrates the problems you run into when you try to build a classification of values using inheritance. But I also said that I wasn't going to really try to explain much, it's been written about plenty.
- bb88 3y ago> This is not a fun situation to deal with and inheritance makes it a lot harder because the "is a" relationship is driving a ton of your logic. Judicious OOP design allows others to change behavior based upon needs that perhaps the original coder never thought of. I would not call most Java OOP design judicious. The control in Java is owned by the library writers and the language devs -- not the people using it.
- inglor_cz 3y agoIn cases of refactoring, inheritance seriously sucks. But it is quite advantageous when, as you say, your model is well thought out and stable.
- sirk390 3y agoWith compositions A uses B but B can never use A. With inheritance Child can use the Parent, but Parent will also call the Child (virtual methods) which in turn can call the Parent again etc.., so the code can become difficult to follow. It can become very complicated with multiple inheritance and multiple levels.
- NickNameNick 3y agoI've certainly done that in the past, but not since I changed jobs ... 7 years ago. Probably not for several years before then.
- mike_hearn 3y agoSuch code would be difficult to follow regardless of whether you use inheritance or not. Sometimes, two pieces of code developed independently just need to interact very closely with each other. Remember that OOP and inheritance to some extent came out of the need to develop GUI systems. Inheritance is still heavily used in GUI toolkits because it's a good fit for that problem space. You have graphs of objects that need to be treated at different levels of abstraction, and controls often need to customize (override) or implement some behavior that shouldn't itself be a part of the public API. Attempts to get rid of inheritance and OOP in UIs end up looking like Compose or React. I found very quickly when working with these that pure composition just wasn't sufficient and these approaches have their own issues; problems that OOP trivially solves become difficult to impossible to solve cleanly without it.
- preommr 3y agoInheritance is fine. It helps to avoid code duplication for logic that requires encapsulated data (private, protected fields). The problem is what if you have a class that needs to derive behavior that's in two (or more) classes? Multi-inheritance is terrible because it becomes a nightmare of which class overrides which. If there's a shallow level of inheritance (1-2 levels deep), then there's nothing wrong with it. Things like composition for most use cases has been common advice since forever. What happened with java was that there was a massive movement for enterprise code that way over-complicated everything based on ideas that didn't pan out. There were all these auxiliary patterns, and ideas that people had to learn to be onboarded onto projects, and so many people poorly understood them that it led to even more spaghetti code, too many people that developed using dogma instead of common sense.
- ickyforce 3y ago"What OOP users claim" -> https://i.imgur.com/KrZVDsP.png https://i.imgur.com/KrZVDsP.png
- nsajko 3y ago"Prefer composition over inheritance" is actually a well-known mantra and appears in, for example, the Design Patterns book from 1994, which is basically the OOP bible. And this is within the OOP bubble, you won't even find inheritance outside of it. https://en.wikipedia.org/wiki/Design_Patterns https://en.wikipedia.org/wiki/Design_Patterns Don't take undergraduate OOP courses seriously.
- chii 3y ago> a well-known mantra which isn't an explanation, but just the same as the OOP mantra that was taught in undergraduate courses.
- zelphirkalt 3y agoOne reason is the unstable base class phenomenon. A some changes in the base class require you to constantly change all your derived classes.
- bbkane 3y agoIf you want to see how codebases work without inheritance in practice, I suggest checking out Go, which features polymorphism through interfaces, but not full-fledged inheritance