• 211lab08_S11
Skip to end of metadata
Go to start of metadata

Lab 08 – Anonymous Classes

Anonymous classes in Java are analogous to lambda-expressions in Scheme. The purpose of this lab is to practice writing such classes. We will use the Full Java language level for this lab.

Lambda-expressions in Java

The abstract concept of a unary function can be modeled by the following interface.

An anonymous Lambdaclass has the following form

Since Object is the superclass of all concrete classes in Java, in practice, we often have to perform type-casting on the arg in order to perform the desired operation. More than often, we need to type-cast the returned result in order to make proper use of it.

Finger exercises:

Use DrJavato compile the above Lambdainterface.
Now create and compile a new file containing the following class.

Add the following method to Lab8Exercises.

(Yes, the code is hard to read because of the "ugly" syntax.) What does it do? Use the Interactions pane to test it out.

Now remove the type cast (Integer) on arg in the above code. Does it compile? Do you see why the type cast is necessary? OK, put back the type cast so that everything compile
Add the following method.

What does it do?
Now remove the first Integer type cast that immediately follows the return in the above code. Does it compile? Put pack the type cast so that everything compiles.

The method in exercise 5 is an example of what is called "Curry-ing"( in honor of the mathematician Curry) where a function of two variables x, y is expressed a function of x whose value is a function of y. Any function of two variables, f (x, y), can be represented as a combination of two functions, each of which is a function of one variable. For example,

  • suppose f(x, y) = x + y;
  • let F be a function of one variable x, such that F( x ) is a function G of one variable y, where G( y ) {{ f(x, y) }} x + y;
  • then F( x ) ( y ) {{ x + y }} f( x, y );

In the same manner, any function of three variables can be represented as a combination of three functions, each of which is a function of one variable via currying.

What does this anonymous inner class do?

IMPORTANT CONCEPT: CLOSURE

Notice in exercise #5 how the anonymous inner class new Lambda inside of x_minus_y is allowed to reference y in its computation? y is said to be in the closure of this anonymous inner class.

In functional programming, the closure of a function (lambda) consists of the function itself and the environment in which the function is defined. In Java, a function is replaced by a class. An inner class is only defined in the context of its outer object (and the outer object of the outer object, etc...). An inner class together with its nested sequence of outer objects in which the inner class is well-defined is the equivalent of the notion of closure in functional programming. Such a notion is extremely powerful. Just like knowing how to effectively use lambda expressions and higher order functions is key to writing powerful functional programs in Scheme, effective usage of anonymous inner classes is key to writing powerful OO programs in Java.

More Exercises

Here is the stub code for ObjectListand its concrete subclasses.

Implement the map method for ObjectList .
Add and implement the following method for class Lab8Exercises.

Add and implement the following method for class Lab8Exercises.

Add and implement the following method for class Lab8Exercises.

Create and compile a file containing the following class

Now write the lookup method for ObjectList .

Add and implement the following method for class Lab8Exercises.


  • No labels