Abstraction
Abstraction is the process of hiding implementation details and showing only what is necessary to the user. It helps developers focus on what an object does rather than how it does it.
In Java, abstraction is a core design principle used to build clean, scalable, and professional applications. Most enterprise-level systems rely heavily on abstraction.
Real-World Understanding of Abstraction
Think about driving a car. You use the steering wheel, accelerator, and brake without knowing how the engine works internally.
You only interact with the features you need. The internal complexity is hidden. That is abstraction.
Why Abstraction Is Important
Abstraction helps Java developers:
- Reduce complexity in large systems
- Improve code readability
- Separate responsibilities clearly
- Make applications easier to extend
Without abstraction, large applications quickly become difficult to manage and maintain.
How Java Achieves Abstraction
Java supports abstraction mainly through:
- Abstract classes
- Interfaces
In this lesson, we focus on abstract classes. Interfaces will be covered in the next lesson.
Abstract Class Explained
An abstract class is a class that cannot be instantiated. It can contain both abstract methods (without body) and normal methods (with body).
Abstract methods define what must be done, while child classes decide how it is done.
Simple Abstract Class Example
Let’s create an abstract class representing a bank account.
abstract class BankAccount {
abstract void calculateInterest();
void showAccountType() {
System.out.println("This is a bank account");
}
}
The method calculateInterest() has no implementation.
Each account type will define it differently.
Implementing the Abstract Class
Now let’s extend the abstract class and provide implementations.
class SavingsAccount extends BankAccount {
void calculateInterest() {
System.out.println("Interest calculated at 4%");
}
}
class CurrentAccount extends BankAccount {
void calculateInterest() {
System.out.println("No interest for current account");
}
}
Abstraction in Action
Even though the reference type is the same, behavior changes based on the object.
public class Main {
public static void main(String[] args) {
BankAccount acc1 = new SavingsAccount();
BankAccount acc2 = new CurrentAccount();
acc1.calculateInterest();
acc2.calculateInterest();
}
}
Java ensures that each account follows the same structure, while allowing different implementations.
Key Rules of Abstract Classes
- Abstract classes cannot be instantiated
- They can contain abstract and non-abstract methods
- Child classes must implement all abstract methods
- They support method overriding and polymorphism
Where Abstraction Is Used in Real Projects
Abstraction is commonly used in:
- Payment gateways
- Banking systems
- Framework design
- Service layers in applications
Modern Java frameworks are built on top of abstraction to remain flexible and extensible.
Key Takeaways
- Abstraction hides internal complexity
- It focuses on what an object does
- Abstract classes define a common structure
- Child classes provide actual behavior
In the next lesson, we will explore interfaces, which take abstraction even further in Java.