MEPX
Chapter 1 of 10All chapters

Chapter 1 of 10

Objects and classes

State plus behaviour in one place.

The unit

An object holds data and the methods that operate on it. A class is the definition; each object created from it carries its own copy of the state.

  • The constructor puts a new object into a valid state before anyone can use it.
  • An object needing several setup calls after construction is badly designed.

Identity and equality

Two objects can hold identical values and still be different objects. Languages separate identity from equality, and forgetting which one a comparison uses causes real bugs.