UpPreviousNext

Subclassing and Receptors

When designing your classes with reuse in mind, you must strike a balance between power and flexibility. If a class has too little functionality, you have little incentive to use and reuse it, and if a class has too much functionality, it often becomes overly specific and difficult to use in other situations. If a class has too much flexibility (through exposed resources and methods), it might be reused, but maintenance benefits to the application are lost.

Subclassing helps to balance functionality, flexibility, and power. Creating a new class by subclassing an existing class allows you to adapt the class to a specific situation, while allowing other developers to leverage their knowledge of the often-reused parent class. Builder Xcessory makes it extremely simple to create subclasses.

Subclassing

All classes created within Builder Xcessory can also be subclassed. A subclass is a class created from another class, which inherits all of the objects, resources, methods, and data members of the superclass while also adding its own.

Making a subclass

To make a subclass, follow these steps:

1. Place Builder Xcessory in Classes view (Browser:Classes view).

2. Select the class you want to subclass.

3. Select Make Subclass from the Browser Edit (or MB3 Quick Access) menu. You are prompted to name the subclass. A new class is created and displayed in the Browser.

Example:
Make a subclass ToggleField

To continue the example that began on Example: Creating an object , you might want to make a subclass ToggleField from the class LabelField, which consists of a form container with a label and a text field as children.

To make a subclass ToggleField from the class LabelField, follow these steps:

1. Confirm the Browser is in Classes view.

2. Select LabelField on the instance hierarchy.

3. Select Make Subclass from the Browser Edit (MB3) menu.

4. You are prompted to name the subclass. Name it ToggleField. The new class is created and displayed in the Browser ( Superclass LabelField and Subclass ToggleField ):

 

Superclass LabelField and Subclass ToggleField


Note: Note that LabelField appears in ToggleField's hierarchy with a colon designating it as the superclass of ToggleField.

5. You can now add methods and data members to your new subclass using the Resource Editor and the Member Editor. (See Adding Methods and Data Members for review.)

Editing superclasses

You can edit any of the exposed resources of the parent, or superclass. Edits made to the superclass propagate to all subclasses. Likewise, in each subclass instance, the default value of an exposed resource can be overridden, as can methods and data members.

Overriding exposed resources

To override the exposed resources of a superclass, use the Browser to select the :superclass object, then use the Resource Editor to specify the new resource values. ( :LabelField is an example superclass in Superclass LabelField and Subclass ToggleField .)

Receptors

Frequently, a major element of a new subclass is one or more additional children. Builder Xcessory allows you to add children when subclassing by designating a container in your superclass as the receptor . Specifying a receptor on a class allows you to extend the object hierarchy of an existing class through its subclasses while not affecting the original class and its class instances.

Making receptors

To specify a receptor (only one is allowed per class), follow these steps:

1. Confirm the Browser is in Classes view.

2. Select the component object that you want to designate as the receptor.

3. Select Make Receptor from the Browser Edit (Quick Access MB3) menu. A small icon, consisting of a square within a square, is placed to the left of the object in the Browser class hierarchy, designating the object as the receptor (for example, see Receptor ).

Any objects subsequently added to a subclass automatically become children of the receptor.

Example: Making the Form widget a receptor

Continuing the previous example, you might want to add to the subclass ToggleField a toggle button not part of the superclass LabelField. The toggle button has the LabelField class's form widget as its parent; therefore, you make the form the receptor.

To make the Form widget a receptor, follow these steps:

1. Confirm that the Browser is in Classes view.

2. Select the Form component within the LabelField hierarchy.

3. Select Make Receptor from the Browser Edit (MB3 Quick Access) menu. The result is shown in the following figure:

 

Receptor

Once you make a component object of a class a receptor, you can add children to any of that class's subclasses.

Adding children to subclasses

To add a child to a subclass, follow these steps:

1. Confirm the Browser is in Classes view.

2. Using MB2, drag the child widget from the Palette and drop it on the object with instance name ":<SuperclassName>" on the desired subclass's instance hierarchy.

The superclass's receptor is the designated parent of any objects added to a subclass.

Adding a toggle button

Continuing the LabelField example, assume you want to include a toggle button to the subclass ToggleField.

To add the toggle button to ToggleField, follow these steps:

1. Confirm the Browser is in Classes view.

2. Drag the toggle button from the Palette.

3. Place the toggle button on the LabelField object on ToggleField's instance hierarchy ( Subclass with Child ):

 

Subclass with Child

UpPreviousNext