home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / OOPTUT34.ZIP / VIRTUAL.TXT < prev    next >
Text File  |  1993-06-01  |  4KB  |  68 lines

  1.              VIRTUAL METHODS in OBJECT-ORIENTED PROGRAMMING.
  2.              ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4.  In the introduction OOPINTRO.TXT, it was observed that inherited methods
  5.  are sometimes not appropriate to the descendant object. For example,
  6.  a circle could not inherit the method Point.Show, even though the data
  7.  fields and some methods could be correctly inherited from a point.
  8.  Normally when code is compiled, the addresses of any methods are resolved
  9.  at that time, but where methods like 'Show' have the same name for a
  10.  descendant object but need to be different, the decision as to which
  11.  method is appropriate must be delayed until run-time.
  12.  
  13.  Giving a method one name that is shared up and down the object hierarchy,
  14.  with each object in the hierarchy implementing the action in a way
  15.  appropriate to itself is called 'Polymorphism'.  It is achieved by using
  16.  'Virtual Methods', as illustrated in the program RIGHTOOP.PAS where the
  17.  procedures script1 and script2 are declared as virtual by the addition of
  18.  'virtual;' as shown:
  19.                       procedure script1;  virtual;
  20.                       procedure script2;  virtual;
  21.  
  22.  When methods are 'static' (as opposed to virtual) references to the
  23.  procedure or function addresses must be resolved at compile time, so that
  24.  the code can be properly processed.  This is called 'early binding'.
  25.  By comparison, when a method is declared 'virtual', it is compiled in such
  26.  a way that identification of which method to use is delayed until the
  27.  program is run.  This is 'late binding' and involves a table of the
  28.  addresses of the various methods, as mentioned in the next paragraph and
  29.  described in the note VMT.TXT
  30.  
  31.  Two syntactic changes must be made in order to use virtual methods.
  32.  First, every instance of an object type must be initialized using a
  33.  special procedure called a 'Constructor'.  The special effect of using a
  34.  constructor is the creation of a 16-bit field, called a Virtual Method
  35.  Table (VMT) field, in the object type.  Thus instead of a Procedure Init,
  36.  as used for objects without virtual methods, Constructor Init is employed
  37.  as shown:
  38.             constructor Init(SurName : string );
  39.  
  40.  The second change is the inclusion of the keyword 'virtual' at the end of
  41.  the line that declares a virtual method.  Thus in the program RIGHTOOP.PAS
  42.  the procedures script1 and script2 are declared virtual for both the staff
  43.  object and the junior object.  Thus when the procedure xxxxx.action1 calls
  44.  the procedure script1 the appropriate script1 is called. Similarly the
  45.  procedure xxxxx.action2 calls the appropriate script2.
  46.        [xxxxx represents either staff or junior]
  47.  
  48.  Although using virtual methods slows the program and uses memory, it is
  49.  generally recommended as it allows for 'extensibility' - the use of compiled
  50.  code in the form of a unit (.TPU) without having the source code available.
  51.  This technique is illustrated in the program JUNIOROB.PAS which uses a unit
  52.  called 'staffobj', created from the program STAFFOBJ.PAS  It performs
  53.  the same actions as the program RIGHTOOP.PAS mentioned above.
  54.  
  55.  The program JUNIOROB.PAS contains the appropriate 'uses' clause:
  56.  
  57.           uses Crt, staffobj;
  58.  
  59.  whilst the unit staffobj contains the relevant interface and implementation
  60.  sections.
  61.  
  62.  
  63.  See separate note entitled 'Virtual Methods and the Virtual Method Table',
  64.  (VMT.TXT) for further details.
  65.  
  66.  VIRTUAL.TXT
  67.  Revised 31.5.93
  68.