Re: Some questions about inheritance
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 17:44:16 GMT
Responding to Johansson...
It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I know that an abstract class doesn't have any implementaion even if a default implementatiion can be supplied for pure virtual methods. What does it actually mean with saying that an Abstract superclasses define a behavioral pattern?
Try substituting pattern -> responsibility. The abstract superclass defines a behavior responsibility that all descending subclasses must implement somehow.
What does it mean with saying that subclasses should respect the semantics of the superclass?
That refers to the semantics of the responsibility. That is, the semantics defines What the subclasses must do. (In contrast, the subclass implementations, in turn, define How it will be done.)
Code inheritance This form of inheritance could be called "convenience inheritance". The subclass "is not a" superclass, but can use some of its implementation. The functions of the superclass need not be valid for the subclass, and if they are they may have different semantics. Here what does it mean with saying . . .and if they are they may have different semantics?
I am not sure what you are driving at here. The subclass /must/ implement any behavior responsibility defined by the superclass.
I suspect by "code inheritance" you are referring to implementation inheritance where the superclass defines a concrete implementation. Then the subclass does not need to implement the behavior; it inherits the superclass' implementation. In that case it is possible for a subclass to override that implementation by providing its own implementation. However, this is one of those ideas that seemed like a good idea at the time but didn't work out well. One should avoid implementation inheritance when possible and should never override it if it is used.
Is delegation the same as aggregation and composition?
No. Aggregation and composition refer to particular types of logical associations between members of different classes. It applies when there exists a Whole/Part logical relationship between entities. However, that notion of Whole/Part is somewhat different than the notion of decomposition underlying delegation.
Delegation is a technique for managing complexity by subdividing a class abstraction into multiple class abstractions that are related through associations of some sort. Thus delegation is a form of logical decomposition to reduce the level of abstraction and an association simply implements the split.
The nature of delegation often leads to a Whole/Part relationship between the decomposed classes, but that is orthogonal to delegation itself. When delegation involves abstract concepts and/or anthropomorphization of behaviors it is not unusual for there to be no obvious Whole/Part association. For example, as an application of the GoF Strategy pattern one might have:
* R1 1
[Asset] ----------------- [Depreciation]
A
| R2
+----------+--------+---...
| |
[Linear] [DoubleDeclining]One could have subclassed [Asset] directly but delegating the depreciation computation to [Depreciation] allows one to manage the context by the way the R1 association is instantiated. The notion of a Depreciation being part of an Asset doesn't work well, though, especially in this context where the same Depreciation strategy could be applied to multiple [Asset] instances.
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- References:
- Some questions about inheritance
- From: Tony Johansson
- Some questions about inheritance
- Prev by Date: Re: XP and Pair Programming
- Next by Date: Re: XP and Pair Programming
- Previous by thread: Re: Some questions about inheritance
- Next by thread: Re: Some questions about inheritance
- Index(es):
Relevant Pages
|