[previous] [up] [next]     [contents] [index]
Next: Complete Applications Up: MrEd Applications: An Example Previous: Defining a Class

Overriding Methods

We can now derive a new class from pb-session% to extend the behavior of a phone book session. Figure ``Counter'' shows the derivation of the class pb-counted-session% from pb-session%. In the new class, a counter displays the number of phone book searches that have been performed so far.

(define pb-counted-session%
  (class pb-session% ()
    (inherit a-panel) ; We need to access the panel object...
    (rename [basic-refresh-number-info refresh-number-info]) ; and old refresh
    (private [search-counter 0]) ; Counter value
    (public
      [refresh-number-info ; Increment the counter and call old refresh
       (lambda ()
         (set! search-counter (add1 search-counter))
         (send counter-text set-value (number->string search-counter))
         (basic-refresh-number-info))])
    (sequence
      (super-init)) ; Do base class initialization
    (private
     (counter-text (make-object mred:text% a-panel
                                (lambda (self event) #f)
                                "Number of Searches Started"
                                "0")))
    (sequence
     (send counter-text set-editable #f))))
-- Figure: Class definition for a phone book session with a counter --

In the class expression for pb-counted-session%, pb-session% is used as the base class. This means that an instance of pb-counted-session% will have all of the instance variables that are declared in pb-session%. However, to access base class instance variables directly from methods defined in the derived class, the base class variables must be declared with the inherit or rename keyword. Instances variables in an inherit clause are made available in the derived class using their original name, while variables in a rename clause are made available under a new name. In the definition of pb-counted-session%, the instance variable a-panel is inherited so that a counter display can be added to the panel. The instance variable refresh-number-info is renamed to base-refresh-number-info; the variable is renamed because pb-counted-session% also overrides refresh-number-info, but the new method needs to access the original method specified in the base class.

The instance variable refresh-number-info is overridden in pb-counted-session%. This means that references to refresh-number-info in the definition for pb-session% will get the value defined in pb-counted-session% for instances of the latter class. (The override does not occur when making instances of pb-session% directly.) The new method needs to extend the old functionality, rather than replace it completely, so the original refresh-number-info is invoked in the new method by using base-refresh-number-info.

In the first sequence clause for pb-counted-session%, the first expression is (super-init); this invokes the intialization procedure defined in the base class, pb-session%.


[previous] [up] [next]     [contents] [index]
Next: Complete Applications Up: MrEd Applications: An Example Previous: Defining a Class

PLT