Objects are the basic elements of Treescript programs. They are executable entities that contain data and provide methods for manipulating that data. Every object is an instance of a class. Classes are templates from which objects are created. They define the type of data that an object contains and the methods for manipulating that data. Objects are created (or instantiated) via constructors. An object is instantiated by assigning specific values to the field variables defined by the class.
Class Declarations
Class declarations allow new classes to be defined for use in Treescript programs. Classes are declared as follows:
( class ( class-modifiers ) class-name extends-clause implements-clause class-body )
The class modifiers, extends clause, and implements clause are optional. If there are no class modifiers, the parentheses enclosing class-modifiers are omitted.
The class modifiers are abstract, public, and final. An abstract class provides an abstract class declaration that cannot be instantiated. In general, abstract classes are used as building blocks for the declaration of subclasses. A class that is declared as public can be referenced outside its package. If a class is not declared as public, it can be referenced only within its package. A final class cannot be subclassed. A class cannot be declared as both final and abstract.
The extends clause is used to identify the immediate superclass of a class and thereby position the class within the overall class hierarchy. It is written as follows:
extends immediate-superclass
The implements clause identifies the interfaces that are implemented by a class. It is written as follows:
implements ( interface-names )
The interface names consist of a list of one or more interface names.
The class body declares the members of a class. It is written as follows:
member-declarations
The member declarations consists of zero or more of the following declarations:
· field-variable-list
· Constructors and methods
· Static and object initializers
· Inner classes
The field-variable-list is written as follows:
var ( variable-modifiers ) ( field-variable-declarations )
The variable modifiers are optional, and if absent, so are the parentheses enclosing them.
Variable Declarations
Variables are used to refer to objects and primitive data types. They are declared as follows:
· ( variable-modifiers ) type variable-list variable-initialization ;
· ( variable-modifiers ) array dimension-count type variable-list variable-initialization ;
· ( variable-modifiers ) set set-type variable-list variable-initialization ;
· ( variable-modifiers ) list list-type variable-list variable-initialization ;
· ( variable-modifiers ) list variable-list variable-initialization ;
· ( variable-modifiers ) enum enumerated-type-name ( enumeration-list ) ;
The variable modifiers and variable initialization are optional. If the variable modifiers are absent, so are the parentheses enclosing them. A variable’s type may be a primitive data type, class type, or interface type. The variable list is a list of variable names enclosed in parentheses. If there is only one variable name, the parentheses are optional.
The variable initialization consists of an expression yielding a value of the variable’s type. If the variable being declared is an array, it can be assigned to an array initializer. An array initializer is a list of expressions which all evaluate to values of the indicated type, enclosed in parentheses. For multi-dimensional arrays, the expression lists are nested to the depth indicated by the dimension count (a positive integer constant). If the array is one-dimensional, the dimension count may be omitted.
When declaring a set, the set type is either an enumerated type or a subrange, such as (.. 'A' 'Z').
There are seven variable modifiers: public, protected, private, static, final, transient, and volatile.
The public, protected, and private modifiers are used to designate the specific manner in which a variable can be accessed. Variables that are declared as public can be accessed anywhere that the class in which they are declared can be accessed. Variables that are declared as protected can be accessed within the package in which they are declared and in subclasses of the class in which they are declared. Variables that are declared as private are only accessible in the class in which they are defined and not in any of its subclasses. If a variable isn’t declared as public, protected, or private it can be accessed only within the package in which it is declared.
A variable that is declared as static is associated with its class and is shared by objects that are instances of its class. A static variable is also known as a class variable.
A variable that is declared as final is a constant and cannot be modified. final variables must be initialized before they are used.
A variable that is declared as transient is not saved as part of an object when the object is serialized. The transient keyword identifies a variable that does not maintain a persistent state.
A variable that is declared as volatile refers to objects and primitive values that can be modified asynchronously by separate threads of execution. They are treated in a special manner by the compiler to control the manner in which they can be updated.
Field Variable Declarations
Field variable declarations are identical to variable declarations, except they may contain an optional property-specifier. Valid property specifiers include:
( property read )
( property write )
( property read write )
Field variable declarations are declared as follows:
· ( variable-modifiers ) property-specifier type variable-list ;
Field variables that include property specifiers are known as properties. Read-only properties cannot be written to except in the class in which they are declared. Write-only properties cannot be read from except in the class in which they are declared. Read-write properties can be read from or written to without restrictions.
Property Methods
Property functions are used to read from read-only (and read-write) properties. Property procedures are used to write to write-only (and read-write) properties. They are declared as follows:
· ( func ( method-modifiers ) field-type func-name throws-clause method-body )
· ( proc ( method-modifiers ) proc-name ( field-type field-name ) throws-clause method-body )
The field-name is the name of the associated property. The field-type is the type of the associated property. The func-name is always get_ concatenated with field-name. The proc-name is always set_ concatenated with field-name.
For read-only properties, if a property function exists, all access to that property is funneled through the property function. For write-only properties, if a property procedure exists, all access to that property is funneled through the property procedure. For read-write properties, all access to that property is funneled through the property function and/or property procedure, if they exist.
Indexed property methods are declared as follows:
· ( func ( method-modifiers ) field-type func-name ( integer-type identifier ) throws-clause method-body )
· ( proc ( method-modifiers ) proc-name ( field-type field-name ; integer-type identifier ) throws-clause method-body )
Constructor Declarations
Constructors are methods that are used to initialize newly created objects of a class. They are declared as follows:
( cons ( constructor-modifiers ) constructor-name ( parameter-list ) throws-clause constructor-body )
The constructor modifiers are optional, and include public, protected, and private. They control access to the constructor and are used in the same manner as they are for variables. If omitted, the parentheses enclosing them are also omitted.
The constructor name is the same as the class name in which it is declared. It is followed by an optional parameter list, consisting of a list of parameter declarations. If there are no parameter declarations, the enclosing parentheses are omitted. Parameter declarations are written as follows:
· type variable-list ;
· array dimension-count type variable-list ;
· set set-type variable-list ;
· list list-type variable-list ;
· list variable-list ;
Each parameter declaration consists of a type followed by a list of parameter names enclosed in parentheses. If there is only one parameter in the list, the parentheses are optional. For array parameter declarations, if the array is one-dimensional, the dimension count may be omitted.
The throws clause identifies all uncaught exceptions that are thrown within the constructor. It is written as follows:
throws ( uncaught-exceptions )
The body of a constructor is written as follows:
local-variable-list do ( constructor-call-statement statements )
The local variable list, constructor call statement, and statements are optional. The local variable list consists of zero or more of the following:
var ( variable-modifiers ) ( variable-declarations )
The constructor call statement allows another constructor of the class or its superclass to be invoked before the constructor’s block body. It is written using one of the two following forms:
· this argument-list ;
· super argument-list ;
The first form results in a constructor for the current class being invoked with the specified arguments. The second form results in the constructor of the class’s superclass being invoked. The argument list consists of expressions that evaluate to the allowed values of a particular constructor.
If no constructor call statement is specified, a default super constructor is invoked before the constructor block body.
Method Declarations
Methods are used to perform operations on the data contained in object. They are written as follows:
· ( proc ( method-modifiers ) method-name ( parameter-list ) throws-clause method-body )
· ( func ( method-modifiers ) return-type method-name ( parameter-list ) throws-clause method-body )
The parameters and throws clause are declared in the same method as in constructor declarations.
The method body differs from the constructor body in that it does not allow a constructor call statement.
The method modifiers include the public, protected, and private modifiers defined for constructors, as well as the final, static, abstract, and synchronized modifiers.
The final modifier identifies a method that cannot be overriden.
The static modifier identifies a class method. Class methods are only allowed to access static class variables. static methods are final.
An abstract method is used to identify a method that cannot be invoked and must be overriden by any non-abstract subclasses of the class in which it is declared. An abstract method does not have a method body.
A synchronized method is a method that must acquire a lock on an object or on a class before it can be executed.