6 Classes and Objects

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 6 | Classes and Objects

6.1 | Creating Interfaces
6.2 | Creating Classes
6.3 | Creating Objects
6.4 | Field and Method Access
6.5 | Mixins
6.6 | Traits
6.7 | Object and Class Contracts
6.8 | Object Equality and Hashing
6.9 | Object Serialization
6.10 | Object Printing
6.11 | Object, Class, and Interface Utilities
6.12 | Surrogates

Racket
top contents ← prev up next →

## 6 Classes and Objects🔗ℹ

> > > Classes and Objects in The Racket Guide introduces classes and objects.

(require racket/class) | package: base

The bindings documented in this section are provided by the racket/class and racket libraries, but not racket/base.

A class specifies

- a collection of fields;

- a collection of methods;

- initial value expressions for the fields; and

- initialization variables that are bound to initialization arguments.

In the context of the class system, an object is a collection of bindings for fields that are instantiated according to a class description.

The class system allows a program to define a new class (a derived class) in terms of an existing class (the superclass) using inheritance, overriding, and augmenting:

- inheritance: An object of a derived class supports methods and instantiates fields declared by the derived class’s superclass, as well as methods and fields declared in the derived class expression.

- overriding: Some methods declared in a superclass can be replaced in the derived class. References to the overridden method in the superclass use the implementation in the derived class.

- augmenting: Some methods declared in a superclass can be merely extended in the derived class. The superclass method specifically delegates to the augmenting method in the derived class.

An interface is a collection of method names to be implemented by a class, potentially with default implementations some methods, combined with a derivation requirement. A class implements an interface when it

- declares (or inherits) a public method for each method in the interface (that does not have an implementation in the interface);

- is derived from the class required by the interface, if any; and

- specifically declares its intention to implement the interface.

A class can implement any number of interfaces. A derived class automatically implements any interface that its superclass implements. Each class also implements an implicitly-defined interface that is associated with the class. The implicitly-defined interface contains all of the class’s public method names, and it requires that all other implementations of the interface are derived from the class. When a class implements an interface but does not explicitly declare an implementation of a method that has a default implementation in the interface, then the default implementation is used for the class.

A new interface can extend one or more interfaces with additional method names; each class that implements the extended interface also implements the original interfaces. The derivation requirements of the original interface must be consistent, and the extended interface inherits the most specific derivation requirement from the original interfaces.

Classes, objects, and interfaces are all values. However, a class or interface is not an object (i.e., there are no “meta-classes” or “meta-interfaces”).

6.1 Creating Interfaces
6.2 Creating Classes
6.2.1 Initialization Variables
6.2.2 Fields
6.2.3 Methods
6.2.3.1 Method Definitions
6.2.3.2 Inherited and Superclass Methods
6.2.3.3 Internal and External Names
6.3 Creating Objects
6.4 Field and Method Access
6.4.1 Methods
6.4.2 Fields
6.4.3 Generics
6.5 Mixins
6.6 Traits
6.7 Object and Class Contracts
6.8 Object Equality and Hashing
6.9 Object Serialization
6.10 Object Printing
6.11 Object, Class, and Interface Utilities
6.12 Surrogates

top contents ← prev up next →