← Back to Home

Abstraction

Abstraction is an object-oriented programming (OOP) principle that focuses on exposing only essential features of an object while hiding the internal implementation details. It helps in building clean, flexible, and maintainable systems.

This is a high-frequency interview topic, especially when comparing abstract classes vs interfaces.

What Is Abstraction?

  • Showing what an object does
  • Hiding how it does it
  • Focuses on behavior, not implementation
  • Achieved using abstract classes and interfaces

Simple definition: Abstraction hides complexity and exposes functionality.

Why Abstraction Is Important

  • Reduces complexity
  • Improves maintainability
  • Enhances flexibility
  • Supports loose coupling
  • Enables polymorphism

Real-World Analogy

Car

  • You use steering, accelerator, brakes
  • You don’t care how the engine works internally

This is abstraction.

How Abstraction Is Achieved in Java

Java provides two mechanisms:

  1. Abstract Classes
  2. Interfaces

1️⃣ Abstract Class

An abstract class is a class declared using the abstract keyword. It can contain:

  • Abstract methods (no body)
  • Concrete methods (with body)
  • Instance variables
  • Constructors

Abstract Class Example

abstract class Vehicle {
    abstract void start(); // abstract method
    void fuel() {           // concrete method
        System.out.println("Fueling vehicle");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car starts with key");
    }
}
          

Key Rules of Abstract Classes

  • Cannot be instantiated
  • May contain abstract and non-abstract methods
  • Can have constructors
  • Can have instance variables
  • Can extend one class only

2️⃣ Interface

An interface defines a contract that classes must implement.

interface Payment {
    void pay();
}

class CreditCardPayment implements Payment {
    @Override
    public void pay() {
        System.out.println("Credit card payment");
    }
}
          

Key Rules of Interfaces

  • All methods are public and abstract by default
  • Supports multiple inheritance
  • No constructors
  • Variables are public static final

Abstract Class vs Interface (Interview Favorite)

Feature Abstract Class Interface
Methods Abstract + concrete Abstract (default allowed Java 8+)
Variables Instance variables Constants only
Constructors Yes No
Multiple inheritance ❌ No ✅ Yes
Access modifiers Any Public only
Use case IS-A relationship CAN-DO capability

Abstraction vs Encapsulation (Common Confusion)

Aspect Abstraction Encapsulation
Focus What How
Purpose Hide complexity Protect data
Achieved by Abstract class / interface Access modifiers
Level Design Implementation

Abstraction with Polymorphism

Payment p = new CreditCardPayment();
p.pay();
          
  • Interface reference
  • Implementation decided at runtime

When to Use Abstraction

  • Framework design
  • APIs and libraries
  • Large systems
  • When behavior varies but contract stays same

Common Beginner Mistakes

  • Confusing abstraction with encapsulation
  • Overusing abstract classes
  • Forgetting to implement abstract methods
  • Trying to instantiate abstract classes
  • Ignoring interfaces for multiple inheritance

Interview-Ready Answers

Short Answer

Abstraction is the concept of hiding implementation details and showing only essential features.

Detailed Answer

In Java, abstraction is achieved using abstract classes and interfaces. It focuses on defining a contract and hiding internal implementation, enabling loose coupling, flexibility, and easier maintenance.

Key Takeaway

Abstraction defines the contract. Implementation provides the details. Abstraction is essential for scalable, extensible, and maintainable Java applications.

Abstraction Examples in Java

1. Basic Abstraction Using Abstract Class

abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }

    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
          

Explanation

  • Abstract method forces implementation

Output: Dog barks

2. Abstract Class with Concrete Method

abstract class Vehicle {
    void start() {
        System.out.println("Vehicle started");
    }

    abstract void run();
}

class Car extends Vehicle {
    void run() {
        System.out.println("Car running");
    }

    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.run();
    }
}
          

Explanation

  • Partial abstraction

Output:

Vehicle started
Car running
          

3. Cannot Instantiate Abstract Class

abstract class A {}

class Test {
    public static void main(String[] args) {
        // A a = new A();  // Compile-time error
    }
}
          

Explanation

  • Abstract classes cannot be instantiated

4. Abstract Class with Constructor

abstract class A {
    A() {
        System.out.println("Abstract constructor");
    }
}

class B extends A {
    B() {
        System.out.println("Child constructor");
    }

    public static void main(String[] args) {
        new B();
    }
}
          

Explanation

  • Abstract class constructors execute

Output:

Abstract constructor
Child constructor
          

5. Abstract Class with Fields

abstract class Bank {
    double rate;
    abstract double getRate();
}

class SBI extends Bank {
    double getRate() {
        return 6.5;
    }

    public static void main(String[] args) {
        Bank b = new SBI();
        System.out.println(b.getRate());
    }
}
          

Explanation

  • Variables + abstract methods

Output: 6.5

6. Interface for Full Abstraction

interface Payment {
    void pay();
}

class UPI implements Payment {
    public void pay() {
        System.out.println("Paid using UPI");
    }

    public static void main(String[] args) {
        Payment p = new UPI();
        p.pay();
    }
}
          

Explanation

  • Interface provides 100% abstraction

Output: Paid using UPI

7. Interface with Multiple Implementations

interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Circle");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("Square");
    }
}

class Test {
    public static void main(String[] args) {
        Shape s1 = new Circle();
        Shape s2 = new Square();
        s1.draw();
        s2.draw();
    }
}
          

Explanation

  • Same interface, different behavior

Output:

Circle
Square
          

8. Interface Reference Achieving Polymorphism

interface Logger {
    void log();
}

class FileLogger implements Logger {
    public void log() {
        System.out.println("File logging");
    }

    public static void main(String[] args) {
        Logger l = new FileLogger();
        l.log();
    }
}
          

Explanation

  • Abstraction + polymorphism

Output: File logging

9. Abstract Class vs Interface (Method Access)

abstract class A {
    abstract void show();
}

class B extends A {
    void show() {
        System.out.println("Implemented");
    }

    public static void main(String[] args) {
        A a = new B();
        a.show();
    }
}
          

Explanation

  • Abstract class can have state and behavior

Output: Implemented

10. Interface with Default Method (Java 8+)

interface Test {
    default void show() {
        System.out.println("Default method");
    }
}

class Demo implements Test {
    public static void main(String[] args) {
        new Demo().show();
    }
}
          

Explanation

  • Interface can have default methods

Output: Default method

11. Overriding Default Method

interface Test {
    default void show() {
        System.out.println("Default");
    }
}

class Demo implements Test {
    public void show() {
        System.out.println("Overridden");
    }

    public static void main(String[] args) {
        new Demo().show();
    }
}
          

Explanation

  • Default methods can be overridden

Output: Overridden

12. Interface with Static Method

interface Util {
    static void help() {
        System.out.println("Helping");
    }
}

class Test {
    public static void main(String[] args) {
        Util.help();
    }
}
          

Explanation

  • Static methods belong to interface

Output: Helping

13. Abstract Class with Final Method

abstract class A {
    final void show() {
        System.out.println("Final method");
    }
}

class B extends A {
    public static void main(String[] args) {
        new B().show();
    }
}
          

Explanation

  • Final method cannot be overridden

Output: Final method

14. Abstract Class with Multiple Child Classes

abstract class Shape {
    abstract void area();
}

class Rectangle extends Shape {
    void area() {
        System.out.println("Rectangle area");
    }
}

class Circle extends Shape {
    void area() {
        System.out.println("Circle area");
    }
}

class Test {
    public static void main(String[] args) {
        Shape s = new Rectangle();
        s.area();
    }
}
          

Explanation

  • Common abstraction, multiple implementations

Output: Rectangle area

15. Abstract Method Cannot Be Private

abstract class A {
    // private abstract void show();  // Compile-time error
}
          

Explanation

  • Abstract methods must be overridden → cannot be private

16. Abstract Class Implementing Interface

interface A {
    void show();
}

abstract class B implements A {
    abstract void test();
}
          

Explanation

  • Abstract class can partially implement interface

17. Real-World Example (Browser Abstraction)

abstract class Browser {
    abstract void open();
}

class Chrome extends Browser {
    void open() {
        System.out.println("Chrome opened");
    }

    public static void main(String[] args) {
        Browser b = new Chrome();
        b.open();
    }
}
          

Explanation

  • Implementation hidden from user

Output: Chrome opened

18. Abstraction Prevents Tight Coupling

interface DB {
    void connect();
}

class MySQL implements DB {
    public void connect() {
        System.out.println("MySQL connected");
    }
}

class App {
    static void start(DB db) {
        db.connect();
    }

    public static void main(String[] args) {
        start(new MySQL());
    }
}
          

Explanation

  • App independent of DB implementation

Output: MySQL connected

19. Abstract Class vs Concrete Class

abstract class A {
    abstract void show();
}

class B {
    void show() {}
}
          

Explanation

  • Abstract enforces rule
  • Concrete does not

20. Interview Summary – Abstraction

abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }

    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
          

Explanation

  • Hides implementation
  • Exposes behavior
  • Achieved using abstract classes & interfaces

Output: Bark