Extend Class

In SmalltalkLanguage this refers to adding a method to a class in a different package than the package the class lives in. This is different to the inheritance mechanism, and I believe quite different to extending a class in JavaLanguage (yes/no?).

This is one of the real strengths of SmalltalkLanguage, and helps prevent the mushrooming of ParallelClassHierarchies when you need to add another layer on to your model.

DanielPoon

Hmm, I'm no SmalltalkLanguage expert, but what's this "package" thing you speak of?

I guess a package maps to a source file in CPlusPlusLanguage. For a class in CPlusPlusLanguage, one source file contains the class definition. Other source files then implement those the methods in the class.

In SmalltalkLanguage, one package defines the class, and usually some methods. Other packages then add methods to that class. There is however no reference to those additional methods in the original class definition. Class definitions specify data structure, not methods or interfaces.

This sort of thing is possible in ObjectiveCee as well, using categories. Additional methods can be added to a class at runtime, using this syntax:

   @interface NSObject(MyAdditions?)
   - yourself;
   @end

@implementation NSObject(MyAdditions?) - yourself { return self; } @end

Compare to a usual class definition:

   @interface MyKeenObject? : NSObject  {
   }
   ...
   @end

@implementation MyKeenObject? ... @end

Categories provide a flexible, easy way to extend objects that already exist in the system. The only limitations are:

Categories can add no additional instance variables.

Categories, if they implement a method of the original object, zap that method whilst there-- it is impossible to call the 'normal' implementation.

They also break encapsulation - not normally a problem though in my experience

They do? How? Is it different(in terms of encapsulation, etc) from just appending the new method(s) to the original implementation?--JoeOsborn

Perhaps break encapsulation was a bit strong. If you extend a class in Smalltalk, these methods have full access to its instance methods. They are exactly the same as 'just appending the new methods(s) to the original implementation' in C++. In SmalltalkLanguage, the class definition is purely a data structure definition. Methods are defined elsewhere. -- DanielPoon


EditText of this page (last edited March 28, 2003) or FindPage with title or text search