Polymorphism

Polymorphism is an object-oriented programming feature related to inheritance. It consist in that some property or method has the same signature across an hierarchy but its implementation may change, meaning it has an expected behavior, but the way this is achieved may vary depending on more concrete requirements.

When some derived class changes some ancestor's property or method implementation, this is an override.

jOOPL makes polymorphism easy, since any class property or method is overridable, meaning that there is a single requirement: follow the same signature.

A sample of polymorphism with inheritance sample would be:

$namespace.register("jOOPL.Samples");

jOOPL.Samples.A = $class.declare(
   function(args) 
   {
         this._name = args.Name;
   }, 
   { 
       SomeMethod: function() 
      {
              document.write(this._name);
      } 
  }
);
jOOPL.Samples.A = $class.declare(
   function(args) 
   {
          this.$base.$ctor(args);
   }, 
   {
         SomeMethod: function() {
              document.write("Got overridden!");
         }
   },
  jOOPL.Samples.A
);

var someB = $new(jOOPL.Samples.B);
someB.SomeMethod();

Another case would be that the overridden method should call its base implementation too, which is achieved by using $base keyword (see keywords):

SomeMethod: function() {
              this.$_.$base.SomeMethod();
              document.write("Got overridden!");
}

Finally, there's another use case of polymorphism: some method in a base class needs to call the most specialized version of some other method. jOOPL does it by using this.$_.$derived special field/keyword found in any object:

$namespace.register("Sample");

Sample.A = $class.declare(
    function() {},
    {
        MethodX: function() {
            this.$_.$derived.MethodY();     
        },
        MethodY: function() {
             // Class A is inherited by some other one and this method is overriden.
        }
   }
);

Last edited Jan 24, 2013 at 11:55 AM by MFidemraizer, version 8

Comments

No comments yet.