Creation and Initialization

Many object-oriented languages provide a mechanism that ties together the two operations of creation and initialization. Doing so avoids a pair of problems that are otherwise common.

The first problem is creating an object, but failing to ensure that the data fields in the object are properly initialized.

the second problem is creating an object, but initializing the fields more than once.

There are two general approaches to addressing this problem. The first is the idea of class or factory methods, and the second is the notion of a constructor.

[audio] [real] Text to accompany slide10, in Chapter 4 of An Introduction to Object-Oriented Programming