Re: Some questions about inheritance



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



.



Relevant Pages

  • Re: inheritance question
    ... >>is when the superclass provides a concrete implementation one wants ... >>inheritance can't be avoided, then NEVER override it. ... put the shared implementation in one place that each subclass' method ...
    (comp.object)
  • Re: extending enum
    ... inherintance is a process of making modified derivatives. ... Inheritance is the passing of traits to an offspring. ... The creation of a subclass is done by ... superclass and its subclass is one of specializtion/generalization ...
    (comp.lang.java.programmer)
  • Re: inheritance/OO problem
    ... it's a consequence of a superclass not having all ... > features of its subclass. ... Inheritance is one-way, subclasses inherit ...
    (comp.lang.java.programmer)
  • Re: Question about inheritance of private variables
    ... > Most books say private members are not inherited. ... > subclass using public accessor methods define in the superclass. ... application of subtle semantics nuances to the terms inheritance, ...
    (comp.lang.java.programmer)
  • Re: Help with design please
    ... entities from the problem space that are cohesive. ... disconnects in how the solution is viewed by different team members. ... >>relationships with the arrow on the superclass end. ... >>relationship the union of subclass members must be a complete set of the ...
    (comp.object)