Abstraction
Abstraction means hiding unnecessary internal complexity, and showing only what is needed to use something.
- Hide the complexity
- Show simple controls
Real World Example: TV and Remote
Example: TV and RemoteA TV does complex things inside: process signals, control display pixels, manage audio circuits, decode video data. As a user, you don't ask:
- How are pixels controlled?
- How is video decoded?
- How do audio circuits work?
Instead, you just get simple controls: turnOn(), changeChannel(), increaseVolume(). Press a button on the remote, and the complex internal steps run on their own.
Abstraction in Java
Code Exampleclass TV {
public void turnOn() {
// turn on TV
}
public void changeChannel(int ch) {
// change channel
}
private void updateDisplay() {
// complex internal operation
}
}
Public vs Private
public: accessible from outside the class. Shows simple actions to the user, this is whatturnOn()andchangeChannel()are for.private: only accessible inside the class. Hides internal complexity, this is whereupdateDisplay()and other internal steps live.
Class Diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TV โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ + turnOn(): void โ
โ + changeChannel(ch): void โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ - updateDisplay(): void โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
+ marks the public methods a user can call. - marks the private methods hidden inside the class, doing the complex work behind the scenes.
Key Idea: Abstraction = Hide Complexity + Show Essentials.
Exam Focus Points
- Abstraction hides implementation details, exposing only what the user needs to interact with an object.
- Public methods form the simple interface; private methods carry out the hidden internal work.
- Abstraction and Encapsulation are related but different: Encapsulation protects data, Abstraction hides complexity.
Summary
- Abstraction hides unnecessary internal complexity from the user.
- Simple, public actions (
turnOn(),changeChannel()) are exposed; complex internal steps stayprivate. - The user only needs to know what an action does, not how it works internally.
- Abstraction = Hide Complexity + Show Essentials.