4 ms·
Interfaces give polymorphism. Implementation inheritance gives method reuse. If you think about the term polymorph ("many shapes"), that describes the world o
by frou_dh 9y ago
Interfaces give polymorphism. Implementation inheritance gives method reuse.
If you think about the term polymorph ("many shapes"), that describes the world of differing objects that have the opportunity to satisfy an interface.
- ahartmetz 9y agoThat's a nice way to think about it. One of the things that still slow me down sometimes when designing something in C++ is its somewhat muddled concepts. The overlapping concepts make it harder to divide and conquer the problem to solve. Rust is quite different regarding interfaces and benefits from many more years of research and experience, so I have reason to assume it does better.
- sanderjd 9y agoFWIW: Java is a language that is more of a contemporary of C++ but gets this much more right. While earlier Java code I've seen seems more like most C++ in the muddiness of the lines between polymorphism and code re-use, most newer Java code I see these days has very flat inheritance hierarchies (if any at all), and makes far more broad use of interfaces and composition. My point is just that it's not only "modern" languages like Rust that have better support than C++ for this distinction.
- marcosdumay 9y agoJava is a decade younger than C++. Also, I haven't ever seen deep C++ hierarchies. That seems to be invented for Java.
- HelloNurse 9y agoProbably you have only seen good and/or recent C++ code. Deep inheritance hierarchies are easy and cheap in Java (in fact, Java is deliberately designed to be good at them), but their popularity is older and wider; historically the early development of complicated OOP took place mostly in C++, with Java attempting to facilitate further complication with more modern tools. For a well-known (and not too unreasonable) example of C++ deep class hierarchy, see the streams in the standard library of 20 years ago. The Java standard library copies them as closely as possible.
- marcosdumay 9y agoIn retrospect, it's indeed very likely that I have only seen good C++ hierarchies.
- sanderjd 9y ago> Java is a decade younger than C++. I meant "more of a contemporary" as more so than Rust (which my parent comment was talking about). Certainly the design decisions (especially when it comes to object orientation) appear to me to be driven by thinking more contemporaneous with C++ than Rust's do.