EIFFEL

Eiffel Classes

Class



A class is an implementation of an abstract data type. This means that it describes a set of run-time objects, characterized by the features (operations) applicable to them, and by the formal properties of these features.

Such objects are called the direct instances of the class. Classes and objects should not be confused: "class" is a compile-time notion, whereas objects only exist at run time. This is similar to the difference that exists in classical programming between a program and one execution of that program, or between a type and a run-time value of that type.

"Class-Oriented Analysis, Design and Programming" would be a more accurate description of the method. This is a simple example, ACCOUNT, where bank accounts is described.

A class X may become a client of ACCOUNT by declaring one or more entities of type ACCOUNT. Declared in the form of:

acc: ACCOUNT

The term "entity" generalizes the more common notion of "variable". An entity declared of a reference type, such as acc, may at any time during execution become "attached to" an object; the type rules imply that this object must be a direct instance of ACCOUNT -- or, as seen below, of a "descendant" of that class.


An entity is said to be void if it is not attached to any object. By default, entities are void at initialization. To obtain objects at run-time, a routine r appearing in the client class X may use a creation instruction of the form

create acc

which creates a new direct instance of ACCOUNT, and initializes all its fields to default values. A variant of this notation, makes it possible to override the default initializations.

Once the client has attached acc to an object, it may call on this object the features defined in class ACCOUNT.

Two kind of features are:

Routines are further divided into procedures and functions. A procedure which is a command and does not return a value. A function is a query that does return a value.

The language definition guarantees automatic initialization, so that the initial balance of an account object will be zero after a creation instruction. Each type has a default initial value, used for initialization: zero for INTEGER and REAL, false for BOOLEAN, null character for CHARACTER, and a void reference for reference types. The class designer may also provide clients with different initialization options.

The other public features, open, deposit, withdraw and may_withdraw are straightforward routines. The special entity Result, used in may_withdraw, denotes the function result; it is initialized on function entry to the default value of the function's result type.

The secret procedure add serves for the implementation of the public procedures deposit and withdraw; the designer of ACCOUNT judged it too general to be exported by itself. The clause is 1000 introduces minimum_balance as a constant attribute, which will not occupy any space in instances of the class; in contrast, every instance has a field for every non-constant attribute such as balance.

If you need to denote the current object explicitly, you may use the special entity Current. For example the unqualified occurrences of add appearing in the above class are equivalent to Current.add.

In some cases, infix or prefix notation will be more convenient than dot notation. To make this possible it suffices to give the routine a name of the form infix "+" rather than plus; internally, however, the operation is still a normal routine call.

A class describes a data structure, accessible to clients through an official interface comprising some of the class features. Features are implemented as attributes or routines; the implementation of exported features may rely on other, secret ones.

Sample code: