Concepts / Understanding Objects and Object-Oriented Programming

Understanding Objects and Object-Oriented Programming

In object-oriented programming, subroutines are often referred to as methods.

  • Programming

Why the Same Idea Has Two Names

Anyone who starts reading about object-oriented programming quickly runs into a word that seems to replace an older one. Many older texts talk about subroutines, while object-oriented material talks about methods. Before you can read either style comfortably, you need to know whether these two words point to different things or to the same thing seen from different angles.

The short answer is that they describe the same kind of thing. In object-oriented programming, subroutines are often referred to as methods. This article explains what that renaming means in practice, why it happens, and how to keep the two words straight so that you can read both older and object-oriented material without confusion.

Tracing a Named Block of Instructions

Start with a concrete picture. Imagine you are reading a program and find a named block of instructions that does one job, such as printing a receipt. You can run that block by using its name. Follow what happens to this block as you read through the three stages below, and notice which part changes when the writer changes the word they use.

  1. 1
    Stage 1: The block exists as a named set of instructions. Its job is to print a receipt. (Example generated for this article to illustrate the source's receipt-printing picture.)
  2. 2
    Stage 2: Some point in the program runs the block by using its name. Nothing about the block itself has to be rewritten to make this happen.
  3. 3
    Stage 3: The same block is described by a writer in a traditional way as a subroutine, or in an object-oriented context as a method. The description changes, but the named set of instructions is still the same thing.

What do you think happens?

If a writer calls the receipt-printing block a method instead of a subroutine, what changes about the block itself?

Reveal answer
Answer:

Only the label changes. The block is still a named set of instructions that you can run by using its name.

The source states that the label changes but the thing being labeled stays the same. Calling the block a method does not give it a new mechanism; it signals the object-oriented context in which the block is being discussed.

Two Terms, One Underlying Idea

Definition

A subroutine is a named block of instructions that is run from another point in the program. Both the subroutine and the method described below refer to this same kind of named routine.

Definition

A method is the term most often used in object-oriented programming for a named routine of this kind, one that belongs to or works with an object. In an object-oriented context, the same kind of named block that a traditional text would call a subroutine is usually called a method.

The figure below places the two terms side by side. The subroutine is the general category. The method is the name used for that category once objects are part of the discussion. Read the figure as a containment picture: a method is a kind of subroutine, used in a particular context.

Subroutinegeneral word for a namedblock of instructionsMethodsubroutine used inobject-oriented programmingoften referred to as, in object-oriented context
How do 'subroutine' and 'method' nest inside each other, and which one is the broader category?
Key point

Method and subroutine describe the same kind of named routine. The difference lies in the context of use, not in what the routine is.

Why Object-Oriented Writers Say Method

The reason for the shift is one of emphasis. Once objects enter the picture, the routines being discussed are the ones that belong to, or work with, those objects. Calling them methods signals that context. The word therefore carries information: it tells you that the routine is connected to an object, even though the underlying kind of thing is still a named set of instructions.

Worked example

Reading one sentence two ways

Two texts describe the same receipt-printing block. Text A says: 'The subroutine prints the receipt.' Text B, which is about object-oriented programming, says: 'The method prints the receipt.' Decide what has changed between the two sentences.

  1. 1
    Identify the named routine

    In both sentences, the thing doing the printing is a named block of instructions that does one job. Both texts point to the same kind of routine.

  2. 2
    Compare the word choice

    Text A uses the general word subroutine. Text B uses the word method, which the source says is used more often once objects are being discussed.

  3. 3
    Locate the context

    Text B's choice of method signals that the routine belongs to or works with an object. Text A does not tell you that object context.

  4. 4
    State what has not changed

    The routine still prints the receipt in the same way in both texts. Only the label and the context signalled by the label differ.

Result

The routine is the same kind of thing in both sentences. The word method adds the object-oriented context; it does not add a new mechanism.

Where a Method Sits Relative to an Object

Because a method is a routine that belongs to or works with an object, it helps to picture the object as the thing the method is attached to. The figure below shows a method connected to an object by the relationship the source describes: the method belongs to, or works with, that object.

Objectthe thing the routinebelongs to or works withMethodnamed routine ofinstructionsbelongs to or works with
What does an object relate to, and where does a method sit relative to that object?

When you read object-oriented code and see a routine named in connection with an object, you can therefore read it as a method: a named block of instructions tied to that object. The source does not describe the internal details of how objects store their information, so you should not assume more than this relationship from the figure.

Choosing Between Method and Subroutine

A practical rule follows from the source. Use subroutine as the general word when you are describing a named block of instructions in a context that does not involve objects. Use method when the routine belongs to or works with an object, which is the usual case once object-oriented programming is the subject. The table below summarizes the difference in usage.

QuestionSubroutineMethod
Is it the general word?Yes, it is the general word for a named routine of instructionsNo, it is the word used in object-oriented programming
What kind of routine is it?A named block of instructions run from another point in the programThe same kind of named block, belonging to or working with an object
When is it the natural choice?When the discussion is not about objectsWhen the discussion is about objects and the routines that work with them
Does the underlying idea differ?NoNo

Subroutine and method refer to the same underlying idea; the columns differ in usage context.

The question to ask when you meet a routine is simple: does this routine belong to or work with an object? If yes, the object-oriented word method is the natural description. If the discussion is general, subroutine is the broader word and still correct for the same kind of routine.

Mistakes When Reading the Terms

  • Common mistake

    Believing a method is a different kind of thing from a subroutine

    What it looks like

    Reading 'the method prints the receipt' and concluding that the printing works in some completely different way from a subroutine.

    Why it fails

    The source states that method and subroutine describe the same kind of named routine. The difference lies in the context of use.

    Do this instead

    Treat the two words as labels for the same kind of routine, and let the object context guide which word is more natural.

  • Common mistake

    Assuming older texts are describing something unrelated to object-oriented material

    What it looks like

    Setting aside a traditional text because it uses subroutine where a modern book uses method.

    Why it fails

    The two words point to the same underlying idea, so the older text is still describing relevant routines.

    Do this instead

    Recognize the shared idea so that you can read both older and object-oriented material without confusion.

  • Common mistake

    Using method for every routine regardless of whether it is connected to an object

    Why it fails

    Method is the word used in object-oriented programming for routines that belong to or work with objects. The general word for the broader category is subroutine.

    Do this instead

    Ask whether the routine belongs to or works with an object before choosing the word.

Practice Reading the Terminology

EASY

A learner reads two sentences. Sentence 1 comes from an older text: 'The subroutine prints the receipt.' Sentence 2 comes from an object-oriented guide: 'The method prints the receipt.' Explain in your own words whether the two sentences describe different routines, and which word you would use if you were describing the same receipt-printing routine in a discussion about objects.

Hints
  • Ask first whether the source says the two words describe the same kind of routine or different kinds.
  • Think about the difference the source identifies: the context of use, not the underlying idea.
  • In a discussion about objects, which word does the source say is used more often?

Key Takeaways

  • In object-oriented programming, subroutines are often referred to as methods, and the two words describe the same kind of named routine of instructions.
  • Subroutine is the general word; method is the word used once objects are being discussed, because the routine belongs to or works with an object.
  • The difference between the two terms lies in the context of use, not in the underlying idea, so the mechanism of the routine does not change when the word changes.
  • When reading object-oriented material, read method as a named routine tied to an object, and read subroutine as the broader family of ideas it belongs to.