Overriding (factory?) methods and inheritance ...

Bjorn Pettersen BPettersen at NAREX.com
Mon Dec 30 12:10:58 EST 2002


> From: Rocco Rossi [mailto:rockrossi69 at libero.it] 
> 
> I posted a message yesterday about problems I was having when 
> inheriting from base classes that contained methods (would it 
> be correct to call them "factory" methods) which produced 
> instances of the same class (type), like a RationalNumber 
> class or a ComplexNumber class or Vector class, and so on.
> 
> The inherited specialized class would of course have those 
> same methods return values of the base class type, so that in 
> the end one would not be able to exploit the new features of 
> the inherited class for those values ... no code re-use!!!
[...]
> ... Has this problem ever come up before? It seems to me that 
> it is non-trivial, but then again maybe I'm not confronting 
> it in the appropriate manner. Need some help.

It's a well researched problem (google for "contravariant inheritance").
Check out: http://caml.inria.fr/caml-list-ar/0544.html for a good
description of the issues involved (using OCaml). [If I remember
correctly Abadi & Cardelli discussed a partial solution in one of their
Object Calculus papers, but it's been a while so I might be wrong...]

-- bjorn




More information about the Python-list mailing list