spacer
Yehuda Shiran June 29, 2002
Implementing the Integer Version of the ICopyObj Interface
Tips: June 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Eclipse Helios Update Brings New PHP Tools
Internet Explorer 9 Ups Standards Support
JBoss Portal 5 Release Easier to Use

One of the ways to support polymorphism is to define an interface that accepts an object of the type Object, and implement it in classes that support actual types as integer, double, and user-defined classes. The code below defines the interface ICopyObj which is implemented by the class CopyInt. This class defines two methods: the constructor CopyInt() and the interface function Copy(). The main program creates two variables, h1 and h2. The variable h1 is generated through the CopyInt() constructor. The variable h2 is generated through the Copy() operator. We print both variables at the end. Here is the code listing:

  // compile with: jsc interface.js

  interface ICopyObj {
    function Copy() : Object;
  }

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

  var h1 : CopyInt = new CopyInt(8);
  var h2 : CopyInt = h1.Copy();
  print("Original: " + h1.i + " Copy: " + h2.i);
And here is the Command Prompt window, showing the code again (interface.js), the jsc compiler's log messages, and the output of interface.exe:

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:

webref The latest from WebReference.com Browse >
Flashmaps' DynamicLocator: Interactive Maps for Small Areas · Flashmaps' AreaSelector: Interactive Maps for Wide Areas · The DB Mapper: Interactive Street-level Maps of U.S. and Canada
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags