Objects, State, and Responsibility

The Object-Oriented Paradigm organizes a program around objects that keep information and provide operations for using it. You encounter this approach when a program manages several entities with similar capabilities but different current values, such as thermostats in different rooms. The useful question is not simply which values the program stores, but which object should be responsible for checking and changing each value.

An object combines information and actions

An object is a software entity with its own identity, current information, and available operations. Its state records its current situation. Its behaviour determines what you can ask it to do. A thermostat object might store a temperature setting and provide an operation that requests a new setting.


Identity distinguishes one object from another. Two thermostats can both store 20 °C and still represent different devices. Equal stored values do not make them the same object. Conversely, one thermostat remains the same object when its setting changes.


An object need not represent a physical thing. A software document, game session, or search request can also have meaningful information and operations. Choose objects according to the responsibilities the program needs, rather than turning every noun into a separate object.



A class describes a kind of object

A class is a description used to create objects with a common structure and set of operations. An object created from a class is also called an instance of that class. A method supplies one of those operations.


For example, the class Thermostat can specify that each thermostat stores a setting and supports request_setting(t). Creating a thermostat for each of two rooms produces two objects. Their settings can differ even though their available operations come from the same class.


Two thermostat objects have different settings while sharing the same available operation.


Feature

What the class specifies

What can differ between objects

Stored information

Each thermostat has a setting

The current setting

Available operation

Each supports a setting request

The requested temperature

Identity

How this kind of object is created

Which individual object it is


The class describes what its instances support; it does not require every instance to have identical current values. Some languages also support data shared through the class itself. Here, the thermostat setting belongs to each individual object.

Objects take responsibility for valid changes

Encapsulation lets an object manage its information through a controlled interface. For a thermostat, that interface can accept reasonable settings and reject requests outside its allowed range. Code using the object requests a change rather than assigning an unchecked value directly.


Let ss be the current setting, tt the requested setting, and LL and UU the permitted lower and upper limits. A method that rejects out-of-range requests follows this rule:


snew={t,L≤t≤U,s,otherwise.s_{\text{new}} = \begin{cases} t, & L \leq t \leq U,\\ s, & \text{otherwise}. \end{cases}


Example: Suppose the permitted range is 16–28 °C. Requesting 22 °C changes Room A's setting from 20 °C to 22 °C. Requesting 31 °C is rejected, leaving its setting at 22 °C. Room B remains at 23 °C throughout because these requests target Room A's object.


A method call affects the object it targets according to that method's rules. It does not automatically change every object created from the same class.



What to remember

  • An object has identity, state, and behaviour.
  • A class describes objects with a common structure and operations.
  • Separate objects can have equal values without being the same object.
  • Controlled operations let an object take responsibility for valid changes.

Example: Tracking Two Refillable Bottles

A hydration-tracking application uses a Bottle class. Each bottle object stores its own capacity and current water volume, measured in mL. A method contract states the conditions under which an operation succeeds and the change it makes.


For positive requested amounts, the class provides these operations:

  • add(amount) adds the full amount if the resulting volume does not exceed capacity.
  • drink(amount) removes the full amount if that much water is available.
  • A rejected request leaves the stored volume unchanged.

The application creates two separate objects:


Object

Capacity (mL)

Initial volume (mL)

left

750

200

right

500

350


a) Identify the class, the number of bottle objects, and the information stored separately by each object.


The class is Bottle, and there are two bottle objects, reached through the names left and right.


Each object stores its own capacity and current volume. The left bottle stores a capacity of 750 mL and a volume of 200 mL. The right bottle stores a capacity of 500 mL and a volume of 350 mL. Sharing a class gives them common operations, while their stored values belong to the individual objects.


b) The application calls left.add(400), right.drink(125), and left.drink(250), in that order. Find the final volume in each bottle and their combined volume.


First, check the proposed addition to the left bottle:


200 mL+400 mL=600 mL.200\ \text{mL}+400\ \text{mL}=600\ \text{mL}.


This is within its 750 mL capacity, so the request succeeds. After this first call, the left bottle contains ________.


Next, the right bottle has enough water to supply 125 mL. Its remaining volume is:


350 mL−125 mL=225 mL.350\ \text{mL}-125\ \text{mL}=225\ \text{mL}.


Finally, the left bottle has enough water to supply 250 mL:


600 mL−250 mL=350 mL.600\ \text{mL}-250\ \text{mL}=350\ \text{mL}.


The final volumes are 350 mL in the left bottle and 225 mL in the right bottle. Their combined volume is:


350 mL+225 mL=575 mL.350\ \text{mL}+225\ \text{mL}=575\ \text{mL}.


The combined volume is 575 mL. As a check, the initial total was 550 mL, and the net change was 400−125−250=25400-125-250=25 mL, giving the same total.


c) Starting from the result in part b, the application calls right.add(300). Determine whether the request succeeds and state the resulting volume in the right bottle.


The right bottle currently holds 225 mL. Its remaining capacity is:


500 mL−225 mL=275 mL.500\ \text{mL}-225\ \text{mL}=275\ \text{mL}.


Only ________ of additional water can fit. The requested 300 mL exceeds that amount. Equivalently, the proposed new volume is:


225 mL+300 mL=525 mL.225\ \text{mL}+300\ \text{mL}=525\ \text{mL}.


The proposed volume of ________ exceeds the 500 mL capacity. The method therefore rejects the request.


The request does not succeed, and the right bottle still contains 225 mL. The contract rejects the entire request rather than adding only the amount that fits. The left bottle remains unchanged because the call targets the right bottle.

A school application manages individual course registrations using object-oriented programming. Match each programming term to the description that identifies its role.

Match each term
A.

A description used to create entities with a common structure and available operations

B.

An individual software entity with identity, stored information, and available operations

C.

The current values stored by an individual software entity

D.

An operation that can be called through a software entity or its defining type

Method

Object

Class

State

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A parking application executes the following statement. The method requests payment of 18 dollars and returns a transaction identifier. Use the exact variable names or number from the statement to complete the descriptions.


Python
transaction_id = meter.pay(18)


The object receiving the request is referenced by . The dollar amount passed as an argument is . The returned identifier is assigned to .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A game character's energy must remain between 0 and 60 units, inclusive. Which design best makes the character object responsible for preserving this rule?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice