Hacker News new | ask | show | jobs
by cousin_it 3449 days ago
I can buy a language without classes if it gives me these things:

* Interfaces

* Records

* Abstract data types

* All the above should use foo.bar() syntax

Classes provide those things, which is good, but also provide implementation inheritance, which is bad.

1 comments

>> but also provide implementation inheritance, which is bad.

Inheritance isn't bad.

Abusing inheritance is bad.

For the record, you can also abuse composition, polymorphism, operator overloading, free functions, interfaces, records, and abstract data types. I've abused them all and seen them abused as well.

Inheritance is just another tool. Like all tools, we are expected to use them wisely.

Do you know any situations where implementation inheritance is the best tool?
I mentioned one such example about 6 hours before your post.

As the other reply stated, when you mostly want to keep the base class but over-ride a small specific subset of what it does.

E.G.

The base class uses class member functions to do save/load records from a store.

You want to implement a variant of that class which instead works well with a database engine you like.

You can then derive a class and define JUST the storage and recall functions, and inherit everything else.

Interfaces are a better tool for that case IMO. One small utility interface (save/load) and two implementations would be easier to test in isolation and wouldn't suffer from the fragile base class problem.
Yeah, inheritance is the best tool when I want a derived class to implement all the behavior of the super class.