Defining an Object

An object's responsibilities split into what it knows (its state) and what it does (its behavior), and a class is the blueprint that describes both.

Apprentice · C# · OOP · Updated

After this lesson you can

  • Separate what an object knows (its state) from what it does (its behavior)
  • Read a class drawn as a UML class diagram

Key takeaways

  • What an object knows is its state, stored in variables; what it does is its behavior, written as methods.
  • A class is a blueprint for a kind of object. Each object made from it is an instance, with its own state.
  • A UML class diagram is a box with three sections: the class's name, its state with types, then its methods.
  • In UML, - marks a private member and + a public one. State is usually private, and the methods that work with it public.

Once you know an object's responsibilities, you can sort them into two groups: the things the object knows, and the things it can do.

A refrigerator with open doors

Things an object knows: its state

What an object knows is its state: the facts it must remember. A refrigerator needs to remember:

  • its current temperature
  • its capacity (how much it can hold)
  • how much space is used

In code, an object's state is stored in variables that belong to it.

Things an object does: its behavior

What an object can do, or what can be done to it, is its behavior. A refrigerator can:

  • open its door
  • add an item
  • remove an item

In code, an object's behavior is its methods. Notice that behavior usually uses or changes state: adding an item changes how much space is used, and the refrigerator should refuse an item that doesn't fit.

A class is a blueprint

A class describes a kind of object: what every object of that kind knows and does. It's a blueprint, or a template. The class Refrigerator describes refrigerators in general; each actual refrigerator you make from it is an object, also called an instance of the class.

From one blueprint you can make many objects. Two refrigerators made from the same class each have their own temperature and their own contents.

Drawing a class: UML

Developers sketch classes before they write them, using UML (the Unified Modeling Language). A class is drawn as a box with three sections:

A UML class diagram of Refrigerator: its name, then its fields, then its methods

Section Holds Refrigerator
Top The class's name Refrigerator
Middle What it knows: its state, with types - temperature: int, - capacity: int, - usedSpace: int
Bottom What it does: its methods + AddItem(size: int): bool, + RemoveItem(size: int)

The symbol in front of each member says who can use it: - means private (only the object itself) and + means public (anyone). State is usually private, and the methods that work with it are public. You'll see why in the next lessons.

Check yourself

2 questions to try on the lesson page.

So what

Every class you write answers two questions: what does this object need to know, and what does it need to do? Answer them on paper, in a UML box, before you write a line of code.

Key terms

Cheat sheets