Interview Q&A / Object-Oriented Programming

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do you achieve abstraction in Java?

SHORT ANSWER

Use abstract classes (partial implementations with abstract methods) or interfaces (pure contracts). Callers depend on the abstract type and never see the concrete class behind it.

Explain it simply

When you book on IRCTC you choose "pay", not "talk to this particular bank over this protocol". The app presents one idea, payment, and hides the many ways it happens.

In Java you declare the idea as an interface or abstract class, write concrete classes for each way, and let calling code hold only the abstract type.

Example

java
abstract class Shape {
    abstract double area();                       // no body
    void describe() { System.out.println("Area = " + area()); }
}

class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    double area() { return Math.PI * r * r; }
}

class Rect extends Shape {
    double w, h;
    Rect(double w, double h) { this.w = w; this.h = h; }
    double area() { return w * h; }
}

Shape s = new Rect(4, 5);
s.describe();   // prints: Area = 20.0

new Shape() is a compile error, because the idea is incomplete.

What to say in the interview

"I achieve abstraction with interfaces for pure contracts and abstract classes when I also want shared state or code. Client code programs to the abstract type, so implementations can change without breaking it."

Common mistakes

  • Believing abstract classes give 100% abstraction while interfaces do not. Modern interfaces can have default methods too.

#abstraction#java#interfaces

Related questions

Practise more Object-Oriented Programming questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Object-Oriented Programming quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.