[Grace-core] [less important] Classical Inheritance vs Delegation

James Noble kjx at ecs.vuw.ac.nz
Fri Mar 8 22:54:34 PST 2013


Some other notes from today's discussion - Kim on "classical inheritance"

A class definition is a template for instances
 - instance variables 
 - method suite
 - some kind of parameterised initialisation code

Single Inheritance:
A subclass definition includes all of the above class definition
 - along with a single superclass
 - its method suite may override the superclass's methods
 - initialisation code in the superclass runs before the code for the subclass
       with "self" bound to the whole new object
 - initialisation code can make self-calls, these should have the same semantics
       as normal method calls

Classes do not have to be objects --- they can be separate, static, non-first class.

BUT: classes as objects in so far as they are factories - that "new" is a standard
method call - is important and good.

James hypothesized that these "classical semantics" capture many common use cases for inheritance well. 


Andrew says: delegation is simpler

James says the big lesson from Self was self sufficient objects, not delegation. 
Delegation allows vampires --- malevolent inheritance from pre-existing objects.
Delegation also often requires complex copy-down semantics or extralinguistic
support (exhibit 1: Self)


On reflection, J says delegation requires *two* implicit parameters on every method call,
or at least on every method call involving delegation:
 - self  (the binding of self)
 - delegate  (the partial object to search for messages)   

Classical OO requires only one - self - super calls can be statically invoked passing 
just self along, assuming all classes can somehow name the superclasses' methods.
The C++ SuperClassName::methodname(x) style is classic here.


I think the factory method design also effectively has two parameters, like delegation
 - self - the binding of the new object to be created, which is passed in as the *extra magic parameter*
 - delegate (the partial object that supplies definitions) - which is passed in as the linguistic "self"

In other words, as Andrew kept repeating from the Treaty of Orlando:  inheritance is delegation between classes!



More information about the Grace-core mailing list