| > Inheritance is presented everywhere as the go-to method for structuring everything in languages supporting inheritance. The whole java class library consists of huge class hierarchies. That conclusion holds if your primary experience is Java: the Java class library is widely believed to have abused inheritance. However other languages have avoided that trap. Apple's Cocoa frameworks in ObjC do use inheritance but also delegation, notifications, etc. Also Swift supports inheritance, but here we see Lattner describing inheritance as a "tool in our toolbox," not as the "go-to method for structuring everything." > Inheritance is everything else but safe: you have to check if you broke the behavior of all methods and public fields you are inheriting. Designing a class interface intended to be inherited is like any other API design exercise. Your API commits to invariants, and it's the client programmer's responsibility to follow them; if they do the class should not be broken. If you find yourself checking "all methods and public fields," either the API is bad or you've misunderstood it. Again, it's just one API design tool. Sometimes the alternative to inheritance is just an ad-hoc, bug-ridden re-implementation of inheritance. |