Data Abstraction and Encapsulation

 

1. What is Data Abstraction?

Data Abstraction is the process of hiding internal implementation details and showing only the essential features of an object.

🎯 Purpose:

  • To reduce complexity.

  • To focus only on what an object does rather than how it does it.


✅ Example of Data Abstraction:


abstract class Animal { abstract void makeSound(); // abstract method - no implementation } class Dog extends Animal { void makeSound() { System.out.println("Barks"); } } class Main { public static void main(String[] args) { Animal a = new Dog(); a.makeSound(); // Output: Barks } }

💡 Explanation:

  • Animal is an abstract class.

  • makeSound() is an abstract method: only its signature is visible.

  • The actual implementation is hidden in the subclass (Dog).

  • The user (main program) only cares about the method being called, not its internal logic.


🔐 Ways to Achieve Abstraction in Java:

TechniqueDescription
Abstract Class            Contains abstract methods that subclasses must implement.
Interfaces            Define what a class must do but not how. (All methods are implicitly abstract                 unless using default/static.)

2. What is Encapsulation?

Encapsulation is the concept of binding data and methods that operate on that data into a single unit (class) and restricting direct access to some of the object's components.

🎯 Purpose:

  • To protect data from unauthorized access.

  • To allow controlled access through getters and setters.


✅ Example of Encapsulation:


class BankAccount { private double balance; // private field (cannot be accessed directly) // Constructor BankAccount(double initialBalance) { balance = initialBalance; } // Getter public double getBalance() { return balance; } // Setter with condition public void deposit(double amount) { if (amount > 0) { balance += amount; } } }

💡 Explanation:

  • The field balance is private.

  • Access is only allowed via public methods like getBalance() and deposit().

  • This is encapsulation — data is protected from external modification.


🔍 Comparison Between Abstraction and Encapsulation

FeatureData AbstractionEncapsulation
Focus    What an object does    How data is hidden and accessed
Hides    Implementation details    Internal data using access modifiers
Achieved by            Abstract classes and interfaces    Classes, access modifiers, getters/setters
Goal    Reduce complexity    Protect data and ensure safety

Real-world Analogy

ConceptReal-life Example
Abstraction                Using a TV remote – you press buttons (what it does), but don’t know how                 circuits inside work.
Encapsulation        A capsule (medicine) – the ingredients (data) are hidden inside, but you consume         it as a whole.

✅ Summary

  • Abstraction = Hiding internal implementation and showing only essential behavior.

  • Encapsulation = Hiding internal data and providing controlled access through methods.

Comments

Popular posts from this blog

Object Oriented Programming PBCST 304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Object Oriented Programming PBCST 304 Course Details and Syllabus

Type casting in Java