Unit 3 · OOP Concepts

3.7Abstraction

On this page
  1. Abstraction vs encapsulation
  2. Abstraction in design: choosing attributes and methods
  3. Self-check questions

Abstraction means showing only the essential features of an object and hiding the unnecessary details. It answers the question what does it do? and ignores how does it do it?

Examples: you drive a car using the steering wheel and pedals without knowing how the engine works. You press "Send" on a message without knowing how the network delivers it.

WHAT you use: the essential featuressteering wheelacceleratorbrakegear leverHOW it works: hidden detailsfuel injectionignition timinggearbox ratiosbrake hydraulics
Abstraction: the driver uses a simple interface (what it does) while the details (how it works) stay hidden

Abstraction is everywhere in programming

You have been using abstraction since your first program. When you call System.out.println("Hi"), you do not know (or need to know) how Java talks to the operating system to draw letters on the screen. When you call Math.sqrt(2), you do not see the algorithm that calculates square roots. Each method is an abstraction: a name for what it does, which hides how it does it.

Levels of abstraction

Large programs are built in layers, and each layer hides the details of the one below:

Each layer only has to understand the layer directly below it, and only through its public methods. This is what makes it possible to build very large systems: nobody has to keep the whole system in their head at once.

Why abstraction helps

Abstraction vs encapsulation

AbstractionEncapsulation
Design-level idea: decide what to showImplementation-level idea: protect the data
Hides complexityHides data
Achieved with abstract classes and interfacesAchieved with private attributes and getters/setters
"What" the object does"How" the data is kept safe

They work together: encapsulation is one of the tools used to achieve abstraction.

Abstraction in design: choosing attributes and methods

For a Student in a library system, the essential attributes are memberId, name and borrowed books. Height and favourite colour are irrelevant and are left out. For a hospital system the same person would have different essential details. Abstraction depends on the problem.

The two Java tools for abstraction

ToolWhat it gives youMore in
Abstract classA partly finished class: shared code plus abstract methods that each subclass must complete3.8.1
InterfaceA pure contract: a list of methods that any class can promise to provide3.8.2

Both let you write code against a general type (Employee, Payable) and leave the details to the classes that implement it.

Remember

  • Abstraction: focus on what an object does, and hide how it does it.
  • Choose the details to model by asking what the problem actually needs.
  • Encapsulation protects data; abstraction hides complexity. They work together.

Self-check questions

Write your answers first, then open the answers to check them.

  1. Define abstraction with a real-life example.
  2. Give two differences between abstraction and encapsulation.
  3. Which details would you include in a Person class for a bank, and which would you exclude?
Show answers
  1. Abstraction means showing only the essential features of something and hiding the details. Example: a TV remote. You press a button to change the channel without knowing anything about the electronics inside.

  2. Any two:

    • Abstraction is a design idea (decide what to show); encapsulation is an implementation idea (protect the data).
    • Abstraction hides complexity; encapsulation hides data.
    • Abstraction uses abstract classes and interfaces; encapsulation uses private attributes with getters and setters.
  3. Include: name, national identity card number, address, phone number, date of birth, account numbers. Exclude: height, eye colour, hobbies and favourite food, which do not matter to a bank.