4 ms·
Until 1.8 Java’s composition over inheritance story was weak. 1.8 introduced default functions on interfaces, which finally allows shared functionality without
by bluejekyll 8y ago
Until 1.8 Java’s composition over inheritance story was weak. 1.8 introduced default functions on interfaces, which finally allows shared functionality without inheritance. In some senses Java’s patterns in this area have been reset, and many old code bases need to catch up (and probably never will).
- AnimalMuppet 8y agoWell, from where I sit, interfaces are another kind of inheritance - inheritance of API rather than implementation. But once you can have default functions, that to me looks exactly like inheritance of a base class, except that the base is called "interface" instead of "class". Am I missing something?
- bluejekyll 8y agoIt's a difference of inheriting data vs. functionality, so yes, the API is what's being inherited. Looking at the history of inheritence a few languages I know: - C++: multiple inheritance super confusing expressions and understanding of things like diamond inheritance. Initialization becoming a really complex thing to understand. - Java: learning from the mistake of C++, determines multiple inheritance is bad, only allows inheriting data from one super class and then API (and now in 1.8 functionality) from any number of interfaces. - Rust: realizes inheritance of any data is bad, and only allows for any number of Trait's (interfaces) to be implemented on types. There's a difference, and ultimately it's that you're not carrying data via the inheritance chain, only functions. The benefit here being that it's much easier to reason about a type and what data it has that determines how it works in the broader system. This allows for very explicit inclusion of data from other types, as opposed to implicit inclusion with C++ and/or Java. In the end moving away from data inheritance leads to fewer mistakes and easier understanding of the code.
- ncmncm 8y agoQ: Who uses inheritance in C++, anymore? A: Java coders. Inheritance defines a mechanism that is sometimes useful. It is useless as an interface design tool.
- bluejekyll 8y agoWith Java 1.8 you can ask the same thing about Java and the answer would be the same. I didn’t mean to suggest that it’s idiomatic in C++ to create strange inheritance graphs, anyone who’s done it once realizes it’s a bad pattern. The issue is more that the language -allows- it.
- Scarbutt 8y agoExcept that a class can inherit from multiple interfaces.