Re: Some questions about inheritance



Tony Johansson a écrit :
Hello Experts!

I'm not...

I have some questions about inheritance that I want to have an answer to.

It says "Abstract superclasses define a behavioral pattern without
specifying the implementation"
I know that an abstract class doesn't have any implementaion

An abstract class has an incomplete implementation. This of course includes the case of no implementation at all, but is not restricted to - one abstract method is enough to make a class abstract.


even if a
default implementatiion can be supplied for pure virtual methods.

AFAIK, a pure virtual method has no implementation. Anyway, this is a C++ specific notion.


What does it actually mean with saying that an Abstract superclasses define
a behavioral pattern?

An abstract class defines how it's subclasses should behave, but not how this behaviour is implemented. The abstract class define the API, concrete classes implements the API. You could, for example, imagine two subclasses of an abstract List class, one subclass implementing the list with a vector, another one implementing it with a linked list. The client code doesn't need to know which concrete type is effectively used.


What does it mean with saying that subclasses should respect the semantics
of the superclass?

In our List exemple, this means that - List::append(item) should append item at the end of the list, - List::prepend(item) should insert item at the beginning of the list - List::count() should return the number of items in the list

If a subclass of List, let's name it FoolList, was to delete first item when client code calls List::append() and return a random integer when client code calls List::count(), then FoolList would not respect the semantic of List - but your compiler won't notice !-)


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.

This is the case when one subclass only to inherit some parts of implementation, but doesn't respect the semantic of the parent class. It's usually a Bad Thing(tm) to do, specially with statically-typed languages.


Is delegation the same as aggregation and composition?

Nope - but delegation is usually used in combination with aggregation or composition.


Aggregation and composition are about whole/part: an object is 'made of' some other(s) object(s). The main difference is that in the second case, the 'composed' object 'owns' the parts (it's responsible for managing the lifecycle of it's parts).

Delegation means that one object delegates some of it's implementation to another object. This allow more dynamism and less coupling than inheritence.

The common pattern is that a composed or aggregated object delegates part of it's behaviour to one of it's componants.

I recommand you to buy and read - and then read again - the GoF's "Design Patterns" book.

HTH,
Bruno
.



Relevant Pages

  • Some questions about inheritance
    ... I have some questions about inheritance that I want to have an answer to. ... It says "Abstract superclasses define a behavioral pattern without ... What does it actually mean with saying that an Abstract superclasses define ... subclass "is not a" superclass, ...
    (comp.object)
  • abstract class versus interface
    ... I've been brushing up on Java and had a question regarding use of ... abstract class and interfaces. ... instantiated and is generally used as a superclass in hierarchies. ...
    (comp.lang.java.programmer)
  • Re: abstract class versus interface
    ... > I've been brushing up on Java and had a question regarding use of ... > abstract class and interfaces. ... > instantiated and is generally used as a superclass in hierarchies. ... don't forget that an interface ...
    (comp.lang.java.programmer)