Concepts / Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP)

Object-oriented programming models a problem as a set of objects instead of a sequence of tasks, so the program's structure follows the entities of the problem.

  • Programming

Starting From a Lending Library

Imagine you are asked to write a program that keeps track of books, borrowers, and due dates at a small lending library. Where do you begin? Many people's first instinct is to think about what the program must do: check a book out, record a return, send a reminder. That instinct is not wrong, but it points you toward one particular style of design. Object-oriented programming, usually abbreviated OOP, starts from a different question: what are the things in this problem, and how do they relate to one another?

What do you think happens?

When you first design the library program, which should you list first: the actions the program must perform, or the things the situation is made of?

  • The actions the program must perform

  • The things the situation is made of

Reveal answer
Answer:

The things the situation is made of

OOP asks what the things in the problem are and how they relate. Designing well means identifying the entities first, then deciding what each one holds, what each one can do, and how the objects work together.

This article follows one idea through the library scenario. You will see a program built around tasks, then the same problem built around objects, then trace how a single object's information changes as its behaviours run.

Two Ways to Organise a Program

In older styles of programming, the work begins by identifying a computing task that solves the problem. The programmer then looks for a sequence of instructions that accomplishes that task. The program is organised around the procedure, meaning the ordered steps that get something done. This is a task-centred design.

Object-oriented programming reverses the emphasis. Instead of centring the design on tasks, it centres the design on objects. Designing the program means deciding which objects are needed and how they should work together, so that the set of objects as a whole models the problem at hand. Once the objects are in place, the tasks tend to show up as things the objects do, rather than as the starting point of the design.

The left column shows the task-centred shape: the program is a chain of steps, and the logic for each step sits in the procedure that performs it. The right column shows the object-centred shape: the program is a set of entities, and each entity is responsible for the information it holds and the things it can do. The same library problem produces two different program structures depending on which question you started with.

QuestionTask-centred designObject-centred design
Where does design start?At the task the program must accomplishAt the entities in the problem
What is the program organised around?An ordered procedureA set of objects
Where does the logic live?In the procedure that runs the stepsIn the object responsible for that behaviour
How do tasks appear?As the starting structureAs things the objects do

Contrasting the two design starting points for the same library problem

What an Object Is

Definition

An object is an entity that has behaviours, holds information, and can interact with other objects.

The definition names three things, and each one is a part of what an object provides. Behaviours are the things the object can do. Information is the store of data the object holds. Interactions are the ways the object works with other objects. Information and behaviours belong inside the object; interactions describe how it connects to what lies outside it.

  • Behaviours: what the object can do, for example a book being checked out
  • Information: what the object knows, for example a book's title and whether it is currently on loan
  • Interactions: how the object works with other objects, for example a loan referring to the book it concerns

The entity an object stands for does not have to be something you can touch. Software objects can represent real entities, such as a physical book, or abstract entities, such as a due date, as long as they matter to the problem. A reservation or a rule about overdue fines can also be treated as objects, because they carry information and take part in behaviour. The question to ask is whether the thing matters to the problem and can be described by what it knows and what it does.

Tracing One Book Through Its Behaviours

Now watch what happens inside one object over time. The information a Book object holds is not fixed. Calling one of its behaviours can change what it stores. The example below is an original illustration written for this article, applying the three properties of an object to the library scenario.

Worked example

A Book object changing state

A Book object holds a title and a status that is either available or on loan. It offers two behaviours: check out and record return. Trace its information through a check-out and then a return.

  1. 1
    Initial state

    The Book holds the title 'Silent Harbour' and status 'available'. Its information describes the book as sitting on the shelf.

  2. 2
    Call the check-out behaviour

    The check-out behaviour is an action the Book itself performs. It changes the stored status from 'available' to 'on loan'. The title is untouched, because the behaviour only concerns the status.

  3. 3
    State after check-out

    The Book now holds the title 'Silent Harbour' and status 'on loan'. Its information has moved from one state to another as a direct result of a behaviour.

  4. 4
    Call the record-return behaviour

    The record-return behaviour changes the stored status from 'on loan' back to 'available'. The object's information again reflects the situation in the world.

Result

The Book's information moved available, on loan, available. Each change happened because a behaviour ran inside the object, not because an outside procedure reached in and edited the status.

Notice that the Book is responsible for its own status. Whatever part of the program asks for a check-out only needs to trigger the behaviour; the object decides how its information changes.

Objects That Model the Library

Designing well means identifying the entities first, then deciding what each one holds, what each one can do, and how the objects work together. For the library, a reasonable set of entities is a Library, Book, Borrower, and Loan. The Loan is the abstract entity: it is the arrangement that connects a borrower to a book and carries a due date.

Read the diagram as a map of responsibilities. The Library contains many Book objects. Each Borrower can have many Loan objects. Each Loan concerns exactly one Book and carries its own due date. No single procedure has to know everything: each object holds the information relevant to it and connects to the others through its interactions.

Notice that the due date belongs to the Loan, not to the Book. The source's guidance is that a program should model what matters to the problem. In this model, a due date matters to the arrangement of a particular loan, so it is stored there.

Mistakes From Thinking in Tasks

When a task-centred mindset is carried into an object-oriented design, a recognisable pattern appears. One central procedure reaches into shared data and changes it directly. The diagram contrasts that shape with one where each object manages its own information.

  • Common mistake

    Building one central procedure that edits every record

    What it looks like

    A single routine checks out a book by reaching into a shared record and setting its status, then does the same for returns and reminders.

    Why it fails

    The information lives outside the thing it describes, so every part of the program must know how the records are laid out.

    Do this instead

    Give the Book its own check-out behaviour so the object decides how its status changes.

  • Common mistake

    Choosing objects by copying the list of tasks

    What it looks like

    Creating a CheckOut object, a ReturnBook object, and a SendReminder object because those are the steps in the task list.

    Why it fails

    The objects follow the actions rather than the entities of the problem, so the structure no longer resembles the situation.

    Do this instead

    Start from the entities that matter, such as Book, Borrower, and Loan, and let the tasks appear as their behaviours.

  • Common mistake

    Treating abstract things as not worth modelling

    What it looks like

    Leaving the due date as a loose value passed around between procedures because it is not physical.

    Why it fails

    A due date carries information and takes part in behaviour, so it can be an object in its own right when it matters to the problem.

    Do this instead

    Ask whether the thing matters to the problem and can be described by what it knows and what it does.

  • Common mistake

    Placing every piece of information in one large object instead of separating the entities

Sending Messages Between Objects

When one object needs something done, it interacts with another object. In the library model, a Borrower wanting to take out a book does not edit the Book's status directly; it works through the objects' own behaviours. Interactions are the connections between objects, and they are what let a set of separate entities collectively solve the problem.

MEDIUM

Using the library model, describe in one sentence which object should own the rule that a loan becomes overdue when today is after its due date. Then say which objects it must interact with to do so.

Hints
  • Ask which object holds the due date.
  • Consider whether the overdue rule concerns information the Loan already holds.

Key Takeaways

  • Object-oriented programming models a problem as a set of objects instead of a sequence of tasks, so the program's structure follows the entities of the problem.
  • An object is an entity that has behaviours, holds information, and can interact with other objects.
  • Task-centred design starts from a procedure and organises the program around ordered steps; object-centred design starts from the entities and lets tasks appear as things the objects do.
  • Designing well means identifying the entities first, then deciding what each one holds, what each one can do, and how the objects work together.
  • Thinking in tasks often produces one central procedure that reaches into shared data, while objects that manage their own information keep logic close to the data it concerns.