Abstraction
Abstraction
Abstraction is the process of hiding the internal implementation details and showing only the essential features to the user.
In simple words:
Abstraction = Hide the "how", show the "what"
Real-Life Example: ATM Machine
When you withdraw cash from an ATM, you only do things like:
1Insert card → Enter PIN → Enter amount → Collect cashYou do not see how the machine verifies your PIN, contacts the bank server, updates your balance, or dispenses notes. All of that complexity is hidden. You only see what the ATM does, not how it does it.
Abstraction vs Encapsulation
| Abstraction | Encapsulation |
|---|---|
| Hides implementation complexity | Hides data and protects it |
| Focuses on what an object does | Focuses on how data is wrapped and guarded |
| Achieved using abstract classes, interfaces | Achieved using private fields and getters/setters |
| Works at the design level | Works at the implementation level |
How to Achieve Abstraction in Java
1Abstraction2 │3 ├── Abstract Class → 0 to 100% abstraction4 │5 └── Interface → Fully abstract contract (plus default/static methods)1. Abstract Class
A class declared with the abstract keyword. It can have abstract methods (no body) and normal methods (with body). It cannot be instantiated.
abstract class Shape {String color;Shape(String color) {this.color = color;}// Abstract method: no body, child must implementabstract double area();// Normal method: shared by all shapesvoid displayColor() {System.out.println("Color: " + color);}}class Circle extends Shape {double radius;Circle(String color, double radius) {super(color);this.radius = radius;}@Overridedouble area() {return Math.PI * radius * radius;}}class Rectangle extends Shape {double length, width;Rectangle(String color, double length, double width) {super(color);this.length = length;this.width = width;}@Overridedouble area() {return length * width;}}public class Main {public static void main(String[] args) {Shape s1 = new Circle("Red", 7);Shape s2 = new Rectangle("Blue", 10, 5);s1.displayColor();System.out.println("Area: " + s1.area());s2.displayColor();System.out.println("Area: " + s2.area());}}// Output:// Color: Red// Area: 153.93804002589985// Color: Blue// Area: 50.0
The user of Shape only calls area(). How the area is calculated is hidden inside each child class.
Cannot Create an Object of an Abstract Class
Shape s = new Shape("Green"); // Compile-time error: Shape is abstract
Rules of Abstract Classes
| Rule | Explanation |
|---|---|
Declared using abstract | abstract class Shape { } |
| Cannot be instantiated | No new Shape() |
| Can have abstract and normal methods | Mixed behavior allowed |
| Can have constructors, fields | Constructor runs through super() |
| Child must implement all abstract methods | Otherwise child must also be abstract |
Cannot combine abstract with final/private/static on a method | They contradict the purpose of overriding |
2. Interface
An interface is a contract. It defines what a class must do, without saying how. A class uses implements to follow the contract.
interface Payment {void pay(double amount); // public abstract by default}class UpiPayment implements Payment {@Overridepublic void pay(double amount) {System.out.println("Paid ₹" + amount + " using UPI");}}class CardPayment implements Payment {@Overridepublic void pay(double amount) {System.out.println("Paid ₹" + amount + " using Card");}}public class Main {public static void main(String[] args) {Payment p1 = new UpiPayment();Payment p2 = new CardPayment();p1.pay(500);p2.pay(1200);}}// Output:// Paid ₹500.0 using UPI// Paid ₹1200.0 using Card
The caller only knows about Payment.pay(). It does not care which payment type is used internally.
Rules of Interfaces
| Rule | Explanation |
|---|---|
Methods are public abstract by default | No body (except default, static, private) |
Variables are public static final | They are constants |
| Cannot be instantiated | No new Payment() |
| A class can implement many interfaces | Gives multiple inheritance of behavior |
| No constructors | Interfaces hold no object state |
Abstract Class vs Interface
| Feature | Abstract Class | Interface |
|---|---|---|
| Keyword | abstract class | interface |
| Inherited using | extends | implements |
| Methods | Abstract and normal | Abstract, default, static, private |
| Variables | Any type (instance, static) | Only public static final constants |
| Constructors | Yes | No |
| Multiple inheritance | A class can extend only one | A class can implement many |
| Use when | Classes share common state and code | Unrelated classes share a common capability |
When to Use Which?
1Classes are closely related and share code/state? → Abstract Class2 Example: Shape → Circle, Rectangle3 4Unrelated classes need the same capability? → Interface5 Example: Payment → UPI, Card, NetBankingKey Benefits
| Benefit | Explanation |
|---|---|
| Reduces complexity | Users see only what they need |
| Flexibility | Implementation can change without affecting users |
| Loose coupling | Code depends on the abstraction, not the concrete class |
| Security | Internal logic stays hidden |
| Maintainability | Easy to add new implementations |
Interview Definition
Abstraction is an OOP principle that hides implementation details and exposes only the essential functionality, achieved in Java using abstract classes and interfaces.
Remember
Abstraction = Hide implementation + Show only what is necessary