Abstraction is the process of hiding the internal implementation details of a system and showing only the essential features to the user. In simpler terms, it allows you to focus on what an object does instead of how it does it. A real-world example is a TV remote: you know that pressing the power button turns the TV on, but you don't need to know the complex circuitry inside the remote that makes it happen.
In Java, abstraction is not a single keyword but a design principle achieved through two primary mechanisms:
Provides Partial Abstraction (0 to 100%). It can have both abstract methods (without a body) and regular methods (with a body).
Provides 100% Abstraction (before Java 8). It only defines the "contract" that other classes must follow.
An abstract class is a class that is declared with the abstract keyword. It serves as a blueprint for other classes.
new keyword.super() from the child).An abstract method is a method that is declared without an implementation. It only has a signature (name, return type, and parameters).
abstract void sendNotification(String message);
By defining this in a parent class, you are forcing every child class (like EmailApp or SmsApp) to write their own specific way of sending that notification.
Abstraction is fundamental for building large-scale applications for several reasons:
This example shows how an abstract BasePayment class defines the flow, but leaves the specific logic to the subclasses.
Many students confuse these two concepts. Here is the definitive difference:
| Feature | Abstraction | Encapsulation |
|---|---|---|
| Main Goal | Hiding complexity (Implementation hiding). | Hiding data (Data protection). |
| Focus | Focuses on the outer behavior of the object. | Focuses on the inner state of the object. |
| Achieved By | Abstract classes and Interfaces. | Access modifiers (Private, Public). |
Q: Can an abstract class exist without any abstract methods?
A: Yes. You can declare a class abstract even if it has only concrete methods. This is done to prevent anyone from creating an object of that class.
Q: Can we use 'private' or 'static' keywords with abstract methods?
A: No. Abstract methods must be overridden by child classes. Since private methods aren't visible to children and static methods belong to the class (not the object), they cannot be abstract.
Q: Can an abstract class be final?
A: No. An abstract class is useless unless it is inherited, but a final class cannot be inherited. Therefore, they are contradictory.
Abstraction is about creating a "Contract" for your code. It allows you to design high-level logic without worrying about the specifics of every single scenario. By mastering abstraction, you move away from simple coding and begin to engage in true Software Architecture.
Next: Master Java Interfaces (100% Abstraction) →