Defining an Object

An object's responsibilities split into what it knows (its variables) and what it can do (its methods), and a class is the blueprint that describes both.

Apprentice · Java · OOP · Updated

By

Once we have an understanding of what the responsibilities of an object are, we can separate the responsibilities into 2 general categories.

The things the object knows and the things it can do.

A refrigerator: it knows its temperature and how full it is, and it can open its door and take items in or out

Things an object KNOWS

The things an object knows are the state of the object. In other words, the things that the object needs to remember.

Some things a refrigerator may need to "remember" are:

  • Current temperature
  • Refrigerator capacity
  • Used space

This data is stored in the object as variables.

Things an object can DO

The things that an object can do are the methods of an object. Methods are simply functions that belong to an object.

Some things that a refrigerator might be able to do (or that can be done to it) are:

  • Open door
  • Add item
  • Remove item

What is a Class: a blueprint

A Class is a blueprint or a template from which an object is created. In UML (Unified Modeling Language), a class is drawn with three sections.

A UML class box for Refrigerator in three parts: the class name; the variables it knows (temperature, capacity, usedSpace); and the methods it can do (increaseTemperature, decreaseTemperature, openDoor, closeDoor, addItem, removeItem)

Cheat sheets