Inheritance

This is a core object-oriented programming feature letting classes inherit from some other one. That is inheritance allows some class to share same properties and methods from their ancestors.

Inheritance is fully-implemented by jOOPL.

An important remark is jOOPL doesn't implement multi-inheritance, meaning that some class must inherit from another one, never from more than one.

Creating an hierarchy

Using inheritance requires two classes at least. A good sample of that would be declaring a class "A" having a method "SomeMethod" and a class "B" deriving from "A":

$namespace.register("jOOPL.Samples", function () {
    this.A = $def({
        $constructor: function (args) { },
        $members: {
            someMethod: function () {

            },
        }
    });

    this.B = $def({
        $constructor: function (args) {
            // Calling the base constructor
            this.$base.$ctor(args);
        },
        $members: {},
        $extends: this.A
    });

    // The B class instance will have the SomeMethod method from class A.
    var someB = new this.A();
    someB.SomeMethod();
});

Above sample demonstrates a class "B" has a method "SomeMethod" inherited from its base class "A".

Call base constructor

Some classes deriving each one of some other can have logic in their constructors and, currently, jOOPL will not automatically call constructor of base classes.

Although class constructor is called when class is instantiated, the constructor must call base class' one.

Take above hierarchy example, but now "A" constructor takes an argument "name" and does something with it:

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

$global.jOOPL.Samples.A = $def({
   $constructor: function(args) 
   {
        // Stores the name in this class field
         this.$_.name = args.name;
   }, 
   $members: { 
      someMethod: function() 
      {
              document.write(this.$_.name);
      } 
  }
);

$global.jOOPL.Samples.A = $def({
   $constructor: function(args) 
   {
          // Calls the base class' constructor!
          this.$base.$ctor(args);
   }, 
   $members: {  
   },
  $global.jOOPL.Samples.A
);

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

Last edited Apr 6, 2013 at 1:48 PM by MFidemraizer, version 7

Comments

No comments yet.