[Zope3-dev] I've changed my mind about inheritence of interface declarations

Phillip J. Eby pje@telecommunity.com
Wed, 12 Mar 2003 12:36:12 -0500


At 12:18 PM 3/12/03 -0500, Shane Hathaway wrote:
>Barry A. Warsaw wrote:
>>>>>>>"JF" == Jim Fulton <jim@zope.com> writes:
>>     JF> (The situation with subclassing would be better if we had an
>>     JF> explicit way to declare subclassing contracts that spelled out
>>     JF> the promises made to and demanded of subclasses.)
>>Classes that are intended to act as base classes should be designed
>>that way, which means specifying a formal interface for its derived
>>classes.  E.g. Something like C++'s protected methods.
>
>I think Stroustrup would think along those lines.  In C++, virtual methods 
>are safe to override, while non-virtual methods aren't necessarily safe 
>(you can do it, but you might not get what you expected).  And Java has 
>"final" methods (which you can't override). Python has no way to declare 
>anything similar.

For abstract base classes in PEAK, I usually declare both an API and an SPI 
interface.  The API interface is what the base class promises its 
subclasses' instances will provide, so long as the subclass implements the 
SPI interface.  Meanwhile, other framework classes and functions rely 
solely on the API interface.

Of course, Python also doesn't have any way to declare that a class is 
abstract, other than defining a __new__() that checks for whether the first 
argument is the abstract class.  :)  In practice, I don't bother doing 
this, and simply define all the SPI methods in the base class to raise 
NotImplementedError, if there isn't a reasonable default behavior.