Skip to main content
Java Basics
CHAPTER 17 Beginner

Encapsulation and Abstraction

Updated: May 17, 2026
5 min read

# CHAPTER 17

Encapsulation and Abstraction

1. Introduction

Encapsulation and Abstraction are two pillars of OOP that protect and simplify your code. Encapsulation hides the internal data; Abstraction hides the implementation complexity. Together they create robust, secure, and user-friendly software.

2. Encapsulation — Data Hiding

Make fields private and provide controlled access via public getter/setter methods.
java
1234567891011121314
public class BankAccount {
    private double balance;

    public double getBalance() { return balance; }

    public void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) balance -= amount;
        else System.out.println("Insufficient funds!");
    }
}

Why? Direct access (account.balance = -1000) bypasses validation. Getters/setters enforce rules.

3. Abstraction — Hiding Complexity

Show only WHAT an object does, not HOW it does it.
java
123456789101112131415161718
abstract class Payment {
    abstract void processPayment(double amount);
    void printReceipt() { System.out.println("Receipt printed."); }
}

class CreditCardPayment extends Payment {
    @Override
    void processPayment(double amount) {
        System.out.println("Processing credit card: $" + amount);
    }
}

class PayPalPayment extends Payment {
    @Override
    void processPayment(double amount) {
        System.out.println("Processing PayPal: $" + amount);
    }
}

4. Abstract Classes

  • Cannot be instantiated directly.
  • Can have abstract methods (no body) and concrete methods (with body).
  • Child classes MUST implement all abstract methods.
java
123456
abstract class Shape {
    abstract double area();      // Abstract — no body
    void display() {             // Concrete — has body
        System.out.println("Area: " + area());
    }
}

5. MCQ Quiz with Answers

Question 1

Encapsulation uses which access modifier primarily?

Question 2

What provides controlled access to private fields?

Question 3

Can you create an object of an abstract class?

Question 4

An abstract method has:

Question 5

Abstraction means:

Question 6

A class with all private fields and public getters/setters demonstrates:

Question 7

Can an abstract class have constructors?

Question 8

If a class has one abstract method, the class must be:

Question 9

Can an abstract class have concrete methods?

Question 10

What is data hiding?

6. Interview Questions

  • Q: Difference between Encapsulation and Abstraction?
  • Q: Why should fields be private? What problem does it solve?
  • Q: Can an abstract class have a constructor even though it can't be instantiated?

7. Summary

Encapsulation protects data by making fields private and exposing controlled getters/setters. Abstraction hides complexity by defining abstract methods that child classes must implement. Both promote security, flexibility, and maintainability in large codebases.

8. Next Chapter Recommendation

In Chapter 18: Interfaces and Abstract Classes, we'll explore interfaces — Java's mechanism for achieving multiple inheritance and defining contracts.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·