Difference Between Data Hiding and Abstraction in Java

Last Updated : 17 Aug, 2026

Abstraction and data hiding are important concepts in Java used to reduce complexity and protect the internal details of a class. Abstraction focuses on hiding implementation details and exposing only the essential functionality, whereas data hiding focuses on restricting direct access to an object's internal data.

  • Abstraction is mainly achieved using abstract classes and interfaces.
  • Data hiding is mainly achieved using access modifiers such as private.

Abstraction in Java

Abstraction is the process of hiding implementation details and exposing only the essential features or functionality to the user. In Java, abstraction can be achieved using abstract classes and interfaces.

  • Shows only essential functionality.
  • Achieved using abstract classes and interfaces.
Java
abstract class Creature {

    // Abstract method
    abstract void numberOfLegs();
}

class Human extends Creature {

    @Override
    void numberOfLegs() {
        System.out.println("Human has two legs");
    }
}

class Elephant extends Creature {

    @Override
    void numberOfLegs() {
        System.out.println("Elephant has four legs");
    }
}

public class Main {
    public static void main(String[] args) {

        Creature human = new Human();
        Creature elephant = new Elephant();

        human.numberOfLegs();
        elephant.numberOfLegs();
    }
}

Output
Human has two legs
Elephant has four legs

Explanation: Creature defines the abstract behavior numberOfLegs() without providing its implementation. Human and Elephant provide their own implementations. The user only needs to know that each creature can provide its number of legs; the specific implementation is hidden.

Data Hiding in Java

Data hiding is the process of restricting direct access to the internal data of a class. It is commonly achieved by declaring data members as private and providing controlled access through methods such as getters and setters.

  • Provides controlled access through methods.
  • Helps prevent unwanted modification of data.
Java
class BankAccount {

    // Private data member
    private double balance;

    // Constructor
    BankAccount(double balance) {
        this.balance = balance;
    }

    // Getter
    public double getBalance() {
        return balance;
    }

    // Controlled method to deposit money
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}

public class Main {
    public static void main(String[] args) {

        BankAccount account = new BankAccount(10000);

        account.deposit(5000);

        System.out.println("Balance: " + account.getBalance());
    }
}

Output
Balance: 15000.0

Explanation: balance is declared private, so it cannot be accessed directly from outside BankAccount. The getBalance() method provides controlled read access, while deposit() controls how the balance can be modified. This protects the internal state of the object.

Abstraction vs Data Hiding

FeatureAbstractionData Hiding
MeaningHides implementation detailsRestricts direct access to data
Main FocusWhat an object doesProtecting internal data
HidesImplementation detailsData members
Main PurposeReduce complexityProtect object state
Achieved UsingAbstract classes and interfacesprivate and other access modifiers
AccessExposes essential functionalityProvides controlled access to data
ExampleATM hides how transactions are processedBank account hides its balance
Related ConceptInheritance and polymorphismEncapsulation
Primary BenefitSimplicity and flexibilityData protection and controlled modification
Comment