Denis Kirevby Denis Kirev

Abstraction in Object-Oriented Design: Focusing on What Matters

How abstraction helps simplify complex systems by focusing on essential attributes and behaviors. Context-driven examples and practical advice.

2 min read

1.2.3 — Abstraction in Object-Oriented Design

Abstraction is a foundational principle in object-oriented programming and modeling. It's how we manage complexity: by focusing on the essentials and ignoring irrelevant details — always in the context of the problem we're solving.

What is Abstraction?

  • Abstraction: Simplifying a concept to its essential attributes and behaviors within a specific context.
  • It's about creating a model that's just detailed enough for your purpose, but no more.
  • Good abstraction follows the Rule of Least Astonishment: no surprises, no irrelevant details.

Why Context Matters

  • The right abstraction depends on your perspective and goals.
  • The same concept can have different abstractions in different contexts.

Example: Student in an Academic Setting

  • Attributes: Courses currently taking, grades in each course, student ID number
  • Behaviors: Studying, doing assignments, attending lectures
  • Irrelevant: Whether the student has a pet, favorite video game, or kitchen cleaning habits

Example: House Cat from a Cat Owner's Perspective

  • Attributes: Name, color, favorite nap location, microchip number
  • Behaviors: Napping, grooming, catching mice, eating, using the litter box
  • Irrelevant: Cat's favorite YouTube channel, owner's work schedule

Example: Dog from a Dog Owner's Perspective

  • Attributes: Breed, size, hair type, ear shape, color
  • Behaviors: Sleeping, eating, doing tricks
  • Irrelevant: Dog's opinion on cat food, neighbor's car color

How Abstraction Relates to Classes

  • In object-oriented modeling, abstraction is most directly applied to the notion of a class
  • A class is a template for instances of a concept, containing only the essential details
  • The attributes themselves are essential, even if their values change over time

Benefits of Good Abstraction

  • Simplifies class design — more focused, succinct, and understandable
  • Makes systems easier to maintain and extend
  • Reduces cognitive load for anyone reading or using your code

Practical Advice

  • Always define your abstraction in the context of your problem
  • Don't be afraid to update your abstractions if the context or requirements change
  • Focus on what's essential for your purpose; ignore the rest
  • Use the Rule of Least Astonishment: your abstraction should make sense and have no surprises

How do you decide what's essential in your abstractions? Have you ever had to change your model as the context shifted? Share your stories below.

Last updated: June 9, 2024