spacer
Yehuda Shiran June 27, 2002
Defining a Polymorphic Interface
Tips: June 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
HP to Microsoft: Thanks for Nothing
iPhone Remains Left Out as Android Scores Flash
The Year of Living the OpenSocial
Polymorphism can be and very often is implemented with interfaces. We have shown how to define interfaces in Column 110. Interfaces differ from classes in that they don't include any implementation. You cannot extend an interface. You can only implement an interface by another class. You cannot create objects from an interface, only from classes that implement interfaces. To support polymorphism, define an interface that accepts an object of the type Object, and implement it in classes that support actual types as int, double, and user-defined classes. Interface names usually start with I, to distinguish them from classes.

The following example defines the ICopyObj interface. It includes one member, Copy(), which returns a copy of the object of type Object:

  interface ICopyObj {
    function Copy() : Object;
  }
The class CopyInt implements the ICopyObj interface for integers. It includes one property (i), and two methods. One method is the class constructor, CopyInt(). The other method is the interface method, Copy():

  class CopyInt implements ICopyObj {
    public var i : int;
    public function CopyInt(i : int) {
      this.i = i;
    }
    public function Copy() : Object {
      return new CopyInt(i)
    }
  }
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.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

webref The latest from WebReference.com Browse >
Administering RBAC in PHP 5 CMS Framework · xref: Automatic Cross Referencing Script · Book Review: Content Rich
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Gateway Launches New Core i7-powered FX-Series Gaming PCs · Review: Lenovo ThinkPad SL300 · EBay, Alternative Site Holiday Resources for Sellers