ASP.NET AJAX Inheritance

You probably know that C# supports inheritance.  With inheritance, C# supports constructor inheritance of sorts.  This means that you can call a base class’s constructors in the derived class.  If you had the following class:

public class DerivedObject : BaseObject
{
     public DerivedObject() { }
}

This, by nature, doesn’t support calling the base class’s constructor.  Instead, you would do:

Public class DerivedObject : BaseObject
{
    public DerivedObject(): base() { }
}

The base() statement calls the base class constructor.  If the constructor has any parameters, these can be passed along too.  You may not want to call the base class.  There isn’t any point in calling the base constructor, unless the base constructor does something (like pass data to local variables or execute some sort of logic, which the latter isn’t recommended).

ASP.NET AJAX has this too; to define a constructor for a class that inherits from another class, you do:

DerivedJSObject = function() { }

DerivedJSObject.registerClass(“DerivedJSObject”, BaseJSObject);

Now, this scenario involves not calling the base class constructor.  Remember I said in C# it’s OK to not call the base class constructor.  In ASP.NET AJAX, it IS NOT OK to omit this call.  This is because ASP.NET AJAX does some important stuff in the base class call, through a special method.  This method is initializeBase, as in:

DerivedJSObject = function() {
    DerivedJSObject.initializeBase(this);
}

DerivedJSObject.registerClass(“DerivedJSObject”, BaseJSObject);

The initializeBase method takes the instance of the class, followed by any other parameters to pass to the constructor (in an array form of []).  This method is important because JavaScript doesn’t support inheritance.  Because it doesn’t, there are a coupleof ways to setup inheritance in JavaScript.  These options are:

I'm not going to go too in-depth into this because these references do a great job of explaining the concepts.

Published Tuesday, July 07, 2009 6:33 AM by bmains
Filed under: ,

Comments

No Comments