Concepts / Objects and Methods

Objects and Methods

Abstraction is the ability to hide complexity so you can focus on what matters to you and ignore the rest.

  • Programming

Why Abstraction Matters

When you use an object or library, you usually want its useful result, not an explanation of every internal step that produces that result. Abstraction makes this possible by hiding complexity so you can focus on what matters to you and ignore the rest. In programming, this means you can work with an object's methods without understanding all of the internal logic behind those methods.

Abstraction does not remove the internal complexity. It places that complexity behind a usable interface.

Following a Method Request

A method is part of the interface through which you interact with an object. You call the method, provide the information it accepts, and use the result it returns. The internal work remains hidden. For example, when using BeautifulSoup, you can call find() with a tag name or CSS selector. You do not need to handle the tokenizer, tree builder, or search algorithm that operate behind that call.

requestinvokesproducesProgrammercalls a methodfind()tag or selectorInternal processinghidden implementationSearch resultreturned result
How does a request from a user move through an object's method and produce a result without exposing internal details?

Interface and Implementation

The interface is what you see and interact with: the methods you call, the parameters those methods accept, and the results they return. The implementation is everything hidden behind that interface, including the algorithms, data structures, and internal logic that make the methods work. The key to abstraction is keeping these two parts separate.

exposescontainsinteracts withhidesObjectInterfacemethods, parameters,resultsObject useruses the interfaceImplementationalgorithms, datastructures, logic
What can a user see and interact with through an object's interface, and what remains hidden inside its implementation?

Hiding Internal Work

Using a Web-Parsing Library

Suppose you need to find an element in an HTML document. What must you understand when using BeautifulSoup, and what remains hidden?

Choose the interface: You use the find() method and provide a tag name or CSS selector. These are parts of the interface exposed to you.

Ignore hidden processing: You do not need to understand the tokenizer that breaks HTML into pieces, the tree builder that constructs the document structure, or the search algorithm that finds matching elements.

Use the result: You focus on the result returned by the method and continue solving your own web-scraping task.

The method call gives you a usable way to search the document while abstraction hides the internal processing required to perform that search.

performsreturnsInternal search stepstokenizer, tree builder,searchfind()tag or CSS selectorFind matching HTMLmany hidden operationsSearch resultusable output
How does a simple method call hide the multiple internal steps required to perform an operation?

The important change is not that the internal work disappears. Instead, the user interacts with one clear operation rather than directly managing every internal step. This lets the user concentrate on the specific problem of finding information in the HTML.

Two Sides of Abstraction

Abstraction helps both sides of an object’s relationship. Users can use objects and libraries without understanding their internal details. Developers can build objects without knowing all the ways those objects will eventually be used. Developers need to design a clear, useful interface and make sure the implementation behind it works correctly.

rely ondesigncan makepreserves use ofObject usersuse methods and resultsClear interfaceshared point of interactionObject developersdesign interface andimplementationInternal changecan remain hidden
How does abstraction let users rely on what an object does while letting developers change how it works internally?

The developers of BeautifulSoup or urllib do not need to know which web pages a particular programmer will scrape, what data that programmer will extract, or what will happen to that data afterward. They provide a useful interface and an implementation that supports it. The users then apply the library to their own specific problems.

Layered Systems

Real software systems are often built in layers. Your code can use high-level libraries. Those libraries can use lower-level libraries, which can use operating system functions. The operating system can use hardware drivers. Each layer provides an interface to the layer above it and hides the complexity of the layer below it.

When scraping a website, you can focus on the scraping task rather than TCP/IP packets or network hardware. BeautifulSoup can focus on parsing HTML rather than HTTP headers or socket connections. urllib can handle HTTP without requiring its users to think about TCP. The layers divide a complex system into manageable responsibilities.

  • Your code uses high-level libraries.
  • High-level libraries use lower-level libraries.
  • Lower-level libraries use operating system functions.
  • The operating system uses hardware drivers.
  • Each layer exposes an interface and hides lower-level complexity.

Mistakes About Interfaces

  • Treating the interface and implementation as the same thing.

    The interface is the part you interact with. The implementation is the hidden logic that makes the interface work.

    Fix: Identify the methods, accepted parameters, and returned results as the interface. Treat the internal algorithms, data structures, and logic as implementation details.

  • Thinking abstraction means there is no internal complexity.

    The tokenizer, tree builder, and search algorithm still perform internal work; abstraction hides that work from the user.

    Fix: Remember that abstraction hides complexity rather than removing it.

  • Believing a library user must understand the library's entire implementation.

    Abstraction allows users to focus on calling methods and using the results for their specific problem.

    Fix: Learn the interface you need and rely on the library's implementation behind that interface.

  • Designing an object around every possible future use.

    Developers benefit from abstraction because they can build the object without knowing all the ways it will be used.

    Fix: Design a clear, useful interface and make sure its implementation works correctly.

Check Your Understanding

MEDIUM

A library provides a method that returns the information you need from a document. List the parts of this interaction that belong to the interface, then list the kinds of internal work that belong to the implementation. Finally, explain why the library developers could change the internal search algorithm without requiring you to change how you use the method.

Hints
  • Look for the method, the information it accepts, and the result it returns.
  • Use the source example of a tokenizer, tree builder, and search algorithm as implementation details.
  • Ask what the user depends on and what the developer can change behind that dependency.
  1. Objects expose methods as part of an interface. Those methods let users request behavior and use results without managing the hidden implementation. Abstraction separates what an object does from how it does it. Users benefit by focusing on their own problems, while developers benefit by building objects without anticipating every use. Layered systems extend the same idea across libraries, operating system functions, and hardware drivers.

Key Takeaways

  • Abstraction hides complexity so you can focus on the relevant part of a program.
  • An object's interface includes the methods, parameters, and results that users interact with.
  • An object's implementation includes the hidden algorithms, data structures, and internal logic.
  • Users can rely on an interface without understanding its implementation.
  • Developers can change an implementation while preserving the interface that users depend on.