Now let's discuss in greater detail the creation of classes by looking in depth at the code we used to define Point-class.
(define My-class (class (... parameters ...) ... body ...))
The parameters correspond to parameters in constructors in Java. They allow initial values to be passed in when we create an instance of a class. They can be thought of as initial values for undeclared instance variables. (This is unlike Java.) In our Point-class example the parameters are x and y which provide the location of the point. These parameters are like the parameters in a lambda expression, where the keyword class is like lambda.
(define My-class (class (... parameters ...) ([var1 value1] [var2 value2] ... [varN valueN]) ... body ...))
In fact the instance variable bindings are also like let* in terms of scoping rules. Each variable binding is available to successive binding statements. Thus looking at the above, the expression valueN can use the values most recently bound to the variables var1 ... varN-1. A big difference between this model and Java is that, in addition to these explicitly declared instance variable, you are also able to treat the parameter names ... parameters ... just as if they were instance variables.
In Point-class we define only one variable, visible, which tells us whether the point is visible or invisible. We give this variable the initial value #f indicating that the point is not visible. This can be changed using the setVisible method.
(define My-class (class (... class parameters ...) ([var1 value1] [var2 value2] ... [varN valueN]) (Object) ([meth1 (method (a1 a2 ... aM) ... method body ...)] [meth2 (method (b1 b2 ... bN) ... method body ...)] ... more methods ... [methL (method (c1 c2 ... cK) ... method body ...)])))
In our Point-class example the methods are location?, visible?, moveTo, moveRel, and setVisible.
While classes are not the same as ADTs, they provide a particularly easy way for programmers to implement a particular ADT. The combination of state information (variables) and operations (methods) specified by a class definition provides encapsulation for each object and is one of the most unique and attractive features of the of the object-oriented paradigm.