Am I the only person who feels that inheritance in high-level languages is rather unnecessary? I understand the idea behind it, letting you stack classes up in neat little hierarchies and inheriting common functions from higher classes, but I just feel that in the end what it does is too fiddly, and could be done far quicker by simply copying the code straight across, or better yet just making another class which it's would-be children aren't tied to.
Are there any people who make use of inheritance regularly, and think it has it's uses?
Name:
Anonymous2009-08-05 20:16
There are cases where inheritance is the right choice, but I think it's used much more often than necessary.
Inheritance is less useful than people tend to think it is, but not at all because copy/paste exists. Please stop coding forever.
Name:
Anonymous2009-08-05 20:20
copying the code straight across
In most cases this will hurts maintainability. If you simply want to do the equivalent of "copying code straight across" you may want composition, not inheritance. In fact, you should probably prefer composition to inheritance in most cases.
I understand the idea behind it, letting you stack classes up in neat little hierarchies and inheriting common functions from higher classes
I prefer to think of it as inheriting common behaviour, not common functions, to emphasize what the point of it all is.
just making another class which it's would-be children aren't tied to
The point of inheritance is that objects can interact with each other through a common contract (interfaces and abstract classes): a piece of code doesn't have to know exactly what type of object it is dealing with in order to do something useful with it. It's just another form of abstraction.
Some people take it too far, of course… but it's not bad in principle.
Name:
Anonymous2009-08-05 20:41
I think what OP really wants is CLOS, where they abstract methods from classes/objects/structures.
>>8
It works the same everywhere, it causes a post not to get bumped. When someone says that they "forgot their sage", it means that they are apologizing for not having used sage originally, for it is too late now.
Name:
Anonymous2009-08-05 20:58
OP here, I understand that copying and pasting a vast number of times is a bad idea, but I've seen examples of inheritance where a parent class splits into two or three sub-classes. Sectioning out a piece of code and putting it in it's own class, or in a parent class, simply because it appears in two or three places seems a little like overkill to me.
I must admit I forgot about the concept of a function being able to act on a number of different objects without needing to know what kind of object it is.
Name:
Anonymous2009-08-05 21:57
Sectioning out a piece of code and putting it in it's own class, or in a parent class, simply because it appears in two or three places seems a little like overkill to me.
It seems to me that this is a problem with the coder. If you design your code to a system model, such refactoring shouldn't be an issue as the model should already specify how the code should look.
>>7 But a PRNG inheriting from some overarching PRNG class is pointless.
Yeah, how could it possibly be helpful to provide a PRNG superclass that defines an API for PRNGs, or to subclass it for each variation? Oh, wait. It's a great idea.
Inheritance? I'm not into OOP so its useless for me.
Plus reusing code can be done through templates,defines,macros and generic programming.
If you're using code that is called or reused many times it should have its own separate function.