[Sorry, the original statement was not made by TopMind
, it was made by me. FYI, I am not anti-OO in the slightest, and attributing statements written other people to commenting 3rd parties is a very slippery slope. -- LucasAckerman
Inheritance is widely mis-advertised as being fundamental to ObjectOrientedProgramming
. Claiming inheritance is fundamental to OO (as is demonstrably incorrect - there are OO techniques and languages without inheritance), when it's used in different ways for several different things, is not helping anyone to clarify things. It has been argued that delegation (see WhatIsDelegation
) is what's really important and that inheritance is one method of providing it, but the multiplicity of different uses for inheritance seems to continually cloud up related issues.
What different things are done with inheritance (and how)?
The fact that the word delegating can be used to define inheritance kinda proves the point that delegating is the real core issue, inheritance is just an implementation of a specific method of delegation.
- various means of polymorphism and encapsulation:
- delegating state, behavior, or interface definition to a super-class, see InterfaceInheritance and ImplementationInheritance
- delegating behavior to a derived class (virtual method calls)
[this appears true with respect to polymorphism, but see below for comments on NonPolymorphicInheritance
Then why is delegation not part of the "big 3" concepts of OO?
Virtually all early OO languages implemented inheritance, not delegation. And most still do.
Agreed (and nice rhetorical question: that's kind of the point of pointing out the ConfusionAboutInheritance). Perhaps it is because ObjectOrientedLanguages? (afaik) originated with AlanKay's SmalltalkLanguage, which was based on classes and inheritance, instead of other forms of delegation. Some modern derivatives are inheritance free - see PrototypeBasedLanguage for example.
A more sophisticated view of object-oriented programming is held by those with greater expertise in its application.
To what are you referring?
Yeah, way to make a non-comment.
What else? Does NonPolymorphicInheritance
Yes--both as a useful theoretic construct, and as a language feature (PrivateInheritance in CeePlusPlus).
ObjectOrientedSoftwareConstruction identifies 12? valid reasons for subclassing - some of them are to solve problems in the application domain, others in the implementation domain. Not all of the uses require subtyping/polymporphism; some of them are about code/design reuse. (Others require
only subtyping, and involve no code reuse, such as inheritance from an abstract base class or Java interface). However, EiffelLanguage only has one type of subclassing - full inheritance (where the derived class gets the interface and implementation of the base classes). BertrandMeyer argues that adding interface-only inheritance, or implementation-only inheritance to EiffelLanguage would have complicated things needlessly, so they aren't in Eiffel.
Top: I find that a fair percent of OO proponents cling to inheritance as a key to OO. Plus, if you move away from language-provided inheritance, then you are not really using OOP for what it is meant for. The design just becomes a "bunch of pointers" more or less. -- top
Inheritance is not key; you can have OO without inheritance. However, like lots of language features, it sure makes things a heck of a lot more convenient.
Delegation is certainly more than a "bunch of pointers". That's like saying relational is just a "bunch of tables"...it's a rather ignorant attempt to trivialize something which is greater than the sum of its parts...
Agreed, and inheritance-centric languages do not have a monopoly on ObjectOrientedProgramming by any means.
These inheritance-free goodies are examples of a PrototypeBasedLanguage
But prototype-based "cloning" *is* inheritance even more faithful to the biology concept of inheritance than the more familiar type of inheritance.
Maybe so, but prototype-style delegation has no fundamental dependence on cloning. It's just a convenient way to implement inheritance (if you want it), since these languages are without. It's not a necessity by any means. A MetaLanguage like LuaLanguage offers delegation-defining mechanisms with with to implement whatever your preferred OO (or other) semantics are.
It's the other way around, inheritance is merely a form of delegation, delegation is the abstract concept, inheritance is merely an implementation, and not the only way to do delegation.
Depends on how you define "delegation". One can view inheritance as a special case of delegation where the delegate is embedded within the object itself, and the forwarding pointer and such are optimized away. Or, one can view delegation and inheritance as complementary methods of achieving polymorphic code reuse (an abstract concept which unfortunately, doesn't have a shorter unambiguous name). I prefer the second view, simply because it separates the abstract concept of one class reusing implementation from another, from the concrete mechanism of one object holding a pointer to another and forwarding method calls to it (with the additional machinery needed so that the delegating object can still override methods in the delegate if it wants to).
Seems to me that delegation is the abstract concept which, is the shorter unambiguous name. Inheritance is a way to delegate up the class hierarchy, you can define the word inheritance with the word delegation, the alternate is not true. But a class hierarchy isn't needed to do delegation, it's merely one implementation of delegation. There are alternative implementations such as prototyping. Both inheritance and prototyping are forms of delegation, but both are not forms of inheritance, so I argue that delegation is the abstract concept.
[[I agree with you, but unfortunately, the world does not. "Delegation" refers to delegating to an instantiated, contained object, whereas "Inheritance" refers to delegating to an inherited class. More confusing is that with non-virtual inheritance, CeePlusPlus
implements inheritance by instantiating instances of superclasses within the object and delegating method calls to them, so anyone who uses inheritance in C++ is really using syntactic sugar over what most call "Delegation".]]