Java Code Example: Data Abstraction – public, private, protected

Data Abstraction is a fundamental concept in Object-Oriented Programming (OOP) that hides unnecessary details and only exposes relevant functionalities.
In Java, access modifiers (public, private, protected) help achieve data abstraction by controlling visibility.

private – Accessible only within the class (hides sensitive data).
protected – Accessible within the same package and subclasses.
public – Accessible from anywhere.

This example demonstrates data abstraction using different access modifiers in Java.


Java Code Example:

// Base class demonstrating access modifiers
class Person {
    private String ssn = "123-45-6789"; // Private: Hidden from outside classes
    protected String name; // Protected: Accessible in subclasses
    public int age; // Public: Accessible from anywhere

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Public method to access private data (Encapsulation)
    public void displaySSN() {
        System.out.println("SSN: " + ssn);
    }

    // Protected method to display name
    protected void displayName() {
        System.out.println("Name: " + name);
    }
}

// Subclass inheriting from Person
class Employee extends Person {
    private String employeeID;

    // Constructor
    public Employee(String name, int age, String employeeID) {
        super(name, age);
        this.employeeID = employeeID;
    }

    // Public method to display employee details
    public void displayEmployeeInfo() {
        displayName(); // Calling protected method from superclass
        System.out.println("Age: " + age); // Accessing public variable
        System.out.println("Employee ID: " + employeeID);
    }
}

// Main class
public class DataAbstractionExample {
    public static void main(String[] args) {
        // Creating an Employee object
        Employee emp = new Employee("Alice Johnson", 30, "E456");

        // Displaying Employee Information
        emp.displayEmployeeInfo();

        // Accessing public method to get SSN
        emp.displaySSN();

        // Direct access (Illegal: Uncommenting the lines below will cause errors)
        // System.out.println(emp.ssn); // ERROR: ssn is private
        // System.out.println(emp.name); // ERROR: name is protected and only accessible within subclass
    }
}

Expected Output:

Name: Alice Johnson
Age: 30
Employee ID: E456
SSN: 123-45-6789

Code Explanation:

  1. Class Person (Base Class)
    • Defines attributes with different access levels:
      • private String ssnCompletely hidden, accessed only via a method.
      • protected String name → Accessible in subclasses.
      • public int age → Accessible from anywhere.
    • displaySSN() allows controlled access to private data.
  2. Class Employee (Subclass)
    • Inherits Person and has its own private employeeID.
    • Calls the protected displayName() method from Person.
    • Accesses the public attribute age.
  3. Main Class (DataAbstractionExample)
    • Creates an Employee object.
    • Calls public methods to display user information.
    • Attempts to directly access private and protected fields (results in errors).

Key Takeaways on Access Modifiers:

ModifierWithin ClassSubclassSame PackageOutside Class
private✅ Yes❌ No❌ No❌ No
protected✅ Yes✅ Yes✅ Yes❌ No
public✅ Yes✅ Yes✅ Yes✅ Yes

Why Use Data Abstraction?

Encapsulation – Keeps sensitive data secure.
Code Maintainability – Prevents direct modification of internal data.
Controlled Access – Allows external access only through methods.

This Java program demonstrates proper access control and data abstraction, ensuring security and modularity in an OOP system! 🚀