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.
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:
- The user sees buttons such as "Withdraw".
- The user interface code calls
account.withdraw(500). - The account class checks the balance and calls
database.update(...). - The database code deals with files, disks and network packets.
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
- Simpler to use: users of a class learn a few method names, not its internals.
- Easier to change: the hidden "how" can be improved (made faster, or moved to a different database) without changing any code that uses the "what".
- Teamwork: teams agree on the methods (the what) first, and then each team builds its own part independently.
Abstraction vs encapsulation
| Abstraction | Encapsulation |
|---|---|
| Design-level idea: decide what to show | Implementation-level idea: protect the data |
| Hides complexity | Hides data |
| Achieved with abstract classes and interfaces | Achieved 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
| Tool | What it gives you | More in |
|---|---|---|
| Abstract class | A partly finished class: shared code plus abstract methods that each subclass must complete | 3.8.1 |
| Interface | A pure contract: a list of methods that any class can promise to provide | 3.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.
- Define abstraction with a real-life example.
- Give two differences between abstraction and encapsulation.
- Which details would you include in a Person class for a bank, and which would you exclude?
Show answers
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.
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
privateattributes with getters and setters.
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.