Video in progress
What do you understand by abstraction in programming?
Abstraction means showing only the part of a complex system that the user needs and hiding the internal details. In Java this is done mainly with interfaces and abstract classes: the caller knows "what is done", not "how".
📖 Answer
A real-life example: to drive a car, the steering wheel, pedals and gearshift are enough. You don't need to know what is happening inside the engine.
In code:
public interface FileStorage {
String save(String name, byte[] content);
byte[] load(String id);
}
A service works with this interface. Whether the file is written to the local disk, to S3 or to MinIO is the implementation's business. If you move from disk to the cloud tomorrow, the service code doesn't change.
One catch: you have to pick the right level:
- too low (say,
writeBytesToPath): the caller still knows the details; - too high (
doEverything(Object o)): nothing is clear.
Abstraction is not limited to interfaces: a well-named method is an abstraction too. When you write calculateDiscount(order), the 30 lines of if inside it are hidden from the reader.
Sign up to mark studied questions and save your progress.