Methods in ODL

Methods in ODL

The third kind of feature of ODL classes is the method. As in other object-oriented languages, a method is a piece of executable code that may be applied to the objects of the class.

In ODL, we can assert the names of the methods connected with a class and the input / output kinds of those methods. These declarations, called signatures, are like function declarations in C or C++ (as opposed to function definitions, which are the code to implement the function). The code for a method would be written in the host language; this code is not part of ODL.

Why Signatures

Declarations of methods appear along with the attributes and relationships in a class declaration. As is normal for object-oriented languages, each method is related with a class, and methods are invoked on an object of that class. Therefore, the object is a "hidden" argument of the method. This style allows the same method name to be used for various different classes, because the object upon which the operation is performed determines the particular method meant. Such a method name is said to be overloaded.

The syntax of method declarations is similar to that of function declarations in C, with two important additions:

1. Method parameters are specified to be in, out, or inout, meaning that they are used as input parameters, output parameters, or both, respectively. The last two types of parameters can be modified by the method; in parameters cannot be modified. In effect, out and input parameters are passed by reference, while in parameters may be passed by value. Note that a method may also have a return value, which is a way that a result can be produced by a method other than by allotting a value to an out or inout parameter.

2. Methods may raise exceptions, which are special responses that are out-side the normal parameter-passing and return-value mechanisms by which methods communicate. An exception generally shows an abnormal or unexpected condition that will be "handled" by some method that called it (perhaps indirectly through a sequence of calls). Division by zero is an example of a condition that might be treated as an exception. In ODL, a method declaration can be followed by the keyword raises, followed by a parenthesized list of one or more exceptions that the method can raise.

Example : In Figure (a) below, we see an development of the definition for class Movie, last seen in "Relationships in ODL / Inverse Relationships" Figure (a). The methods contained with the class declaration are as follows.

Line (8) declares a method lengthInHours. We might imagine that it creates as a return value the length of the movie object to which it is applied, but converted from minutes (as in the attribute length) to a floating-point number that is the equivalent in hours. Note that this method takes no parameters. The Movie object to which the method is applied is the "hidden" argument, and it is from this object that a possible implementation of lengthInHours would achieve the length of the movie in minutes.

Method lengthInHours may raise an exception called noLengthFound. Most probably this exception would be raised if the length attribute of the object to which the method lengthInHours was applied had an undefined value or a value that could not represent a valid length (e.g., a negative number).

Adding method signatures to the Movie class

In line (9) we see another method signature, for a method called starNames. This method has no return value but has an output parameter whose type is a set of strings. We assume that the value of the output parameter is calculated by starNames to be the set of strings that are the values of the attribute name for the stars of the movie to which the method is applied. However, as always there is no guarantee that the method definition behaves in this specific way.

Eventually, at line (10) is a third method, otherMovies. This method has an input parameter of type Star. A possible execution of this method is as follows. We may assume that otherMovies expects this star to be one of the stars of the movie; if it is not, then the exception noSuchStar is increased. If it is one of the stars of the movie to which the method is applied, then the output parameter, whose type is a set of movies, is given as its value the set of all the other movies of this star.