Abstract Classes

Core.extend() supports the creation of abstract classes and the specification of abstract properties within those classes. When attempting to create a concrete class, Core.extend() will verify that any abstract properties of any base classes have been provided. Classes may also be marked as simply being abstract, even if they have no abstract properties of their own.

Abstract properties are specified in an abstract block, as shown:

Pet = Core.extend({
    
    $abstract: {
        numberOfFeet: null,

        speak: function(loudly) { };
    },

    getInfo: function() {
        return "Feet: " + this.numberOfFeet + ", speak: " 
                + this.speak(false) + ", SPEAK:" + this.speak(true);
    }
});

The values of properties in the $abstract block are actually discarded, so it would be equally safe for us to simply set the value of speak in the $abstract block to null. Though technically unnecessary, providing a function definition with parameters names is a good practice for purposes of documentation and maintainability.

To create a concrete class derived from an abstract class, you must provide all the abstract properties, e.g.:

Dog = Core.extend(Animal, {
    numberOfFeet: 4,

    speak: function(loudly) {
        return loudly ? "WOOF!!!!!" : "woof";
    }
});

Snake = Core.extend(Animal, {
    numberOfFeet: 0,

    speak: function(loudly) {
        return loudly ? "SSSSSSSSSSSSSSSSSS!!!!!" : "sssss";
    }
});

Additionally, one can declare a class as simply being abstract, even if it has no abstract methods of its own. To do this, simply define the value of $abstract to be true in your object definition. The following example demonstrates such a case:

AbstractRodent = Core.extend(Animal, {

    $abstract: true,

    nibble: function() {
        return "nibbling!";
    }
});