spacer
Yehuda Shiran June 30, 2002
Implementing a Polymorphic Utility
Tips: June 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Polymorphism is easy to demonstrate using utility functions. Utility functions are like methods of the main program, or of the global object as it is sometimes called in other languages. You define a utility function in the main program section, before, after, or in between the class definitions. Our utility function, CopyMe(), accepts an object of type Object, and returns a copy of it by using the Copy() method. Here is the utility function:

  function CopyMe(ic : Object) : Object {
    return ic.Copy();
  }
The Copy() method is defined in the interface ICopyObj:

  interface ICopyObj {
    function Copy() : Object;
  }
The following two classes, CopyInt and CopyDouble, implement this interface for integer and double values, respectively:

  class CopyInt implements ICopyObj {
    public var i : int;
    public function CopyInt(i : int) {
      this.i = i;
    }
    public function Copy() : Object {
      return new CopyInt(i)
    }
  }

  class CopyDouble implements ICopyObj {
    public var d : double;
    public function CopyDouble(d : double) {
      this.d = d;
    }
    public function Copy() : Object {
      return new CopyDouble(d)
    }
  }
To learn more about JScript .NET, go to Column 111, JScript .NET, Part V: Polymorphism.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint