OOP

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:

text
1Insert card → Enter PIN → Enter amount → Collect cash

You 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

AbstractionEncapsulation
Hides implementation complexityHides data and protects it
Focuses on what an object doesFocuses on how data is wrapped and guarded
Achieved using abstract classes, interfacesAchieved using private fields and getters/setters
Works at the design levelWorks at the implementation level

How to Achieve Abstraction in Java

text
1Abstraction
2 │
3 ├── Abstract Class → 0 to 100% abstraction
4 │
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 implement
abstract double area();
// Normal method: shared by all shapes
void displayColor() {
System.out.println("Color: " + color);
}
}
class Circle extends Shape {
double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
double 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;
}
@Override
double 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

RuleExplanation
Declared using abstractabstract class Shape { }
Cannot be instantiatedNo new Shape()
Can have abstract and normal methodsMixed behavior allowed
Can have constructors, fieldsConstructor runs through super()
Child must implement all abstract methodsOtherwise child must also be abstract
Cannot combine abstract with final/private/static on a methodThey 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 {
@Override
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI");
}
}
class CardPayment implements Payment {
@Override
public 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

RuleExplanation
Methods are public abstract by defaultNo body (except default, static, private)
Variables are public static finalThey are constants
Cannot be instantiatedNo new Payment()
A class can implement many interfacesGives multiple inheritance of behavior
No constructorsInterfaces hold no object state

Abstract Class vs Interface

FeatureAbstract ClassInterface
Keywordabstract classinterface
Inherited usingextendsimplements
MethodsAbstract and normalAbstract, default, static, private
VariablesAny type (instance, static)Only public static final constants
ConstructorsYesNo
Multiple inheritanceA class can extend only oneA class can implement many
Use whenClasses share common state and codeUnrelated classes share a common capability

When to Use Which?

text
1Classes are closely related and share code/state? → Abstract Class
2 Example: Shape → Circle, Rectangle
3
4Unrelated classes need the same capability? → Interface
5 Example: Payment → UPI, Card, NetBanking

Key Benefits

BenefitExplanation
Reduces complexityUsers see only what they need
FlexibilityImplementation can change without affecting users
Loose couplingCode depends on the abstraction, not the concrete class
SecurityInternal logic stays hidden
MaintainabilityEasy 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

Next TopicCollections Framework: Overview