Overview
English meaning of abstraction: Something that exists only as an idea.
Technical translation: Abstraction is the concept of representing something at a high level, without going into too many details.
Anyone involved in object-oriented programming would already be aware of these definitions. However, it really helps to understand abstraction in depth to leverage its real benefits in your analysis and design.
How Abstraction Makes Life Easy
Consider planning a week's trip to visit your favorite destinations. You need to manage accommodation, route, transportation, food, and many more details. You would want a travel agent to understand what you want and take care of how to plan the trip. Here, the Travel Agent is your abstraction to arrange your trip.
Another practical example is the OSI network layers model. Java provides an abstraction layer for programmers so they don't need to code in the byte-codes of JVM or even machine-level language. The emphasis is on making the client's work easy by introducing one controlling abstraction. Additional layering hides complexity from the current layer from the layers upward in the stack.
Types of Abstraction
Most programming languages like C++, C#, and Java support two types of abstraction: Control Abstraction and Data Abstraction.
Control Abstraction
Abstraction of behavior. Provides an easier, higher-level API to hide clients from unnecessary execution details.
When an orthopedic doctor looks at a patient, they focus on bones and joints. When a dermatologist looks at the same patient, they focus on skin. These are two different abstractions for the same entity (human).
In programming, abstraction can be achieved using abstract classes and interfaces. By declaring an accelerate() method as abstract in a Vehicle class, you indicate that Vehicle is just an idea. Vehicle cannot be instantiated without a concrete implementation. Every vehicle implementation must have some way to accelerate() or doBreak().
Taking abstraction to its extreme results in an interface. Interfaces only define method signatures which must be implemented by any concrete implementation of that interface.
Data Abstraction
Data abstraction focuses on representing data at a high level without exposing internal details. It works with classes and their properties, defining what data matters without revealing how it is stored or manipulated.
Focus on "What"
Abstraction indicates "only idea" – not associated with anything concrete. In other words, it focuses on what and factors out how something should be done.
To implement abstraction in object-oriented programming, you should be able to define just the behavior with very limited or no implementation logic. In Java, this can be achieved by using the abstract keyword at method and class level.
Abstraction from Different Perspectives
Any entity can be seen differently from different perspectives. What is important from the perspective of one client may differ from another.
Consider a Car class with properties like color, brand, model, speed, and position. Different systems might abstract the Car differently:
Car Abstraction with perspective of Vehicle Tracking System:
interface Trackable{
int getXCoordinate();
int getYCoordinate();
}
Car Abstraction with perspective of Driver:
interface Drivable{
void start();
void accelerate();
void reverse();
void doBreak();
String getSpeed();
void stop();
}
By identifying the right abstraction for your system, you can create classes and interfaces that are relevant to your client's perspective and needs.