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.
// 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
}
}
Name: Alice Johnson
Age: 30
Employee ID: E456
SSN: 123-45-6789
Person
(Base Class)
private String ssn
→ Completely 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.Employee
(Subclass)
Person
and has its own private employeeID
.displayName()
method from Person
.age
.DataAbstractionExample
)
Employee
object.private
and protected
fields (results in errors).Modifier | Within Class | Subclass | Same Package | Outside Class |
---|---|---|---|---|
private | ✅ Yes | ❌ No | ❌ No | ❌ No |
protected | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
public | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
✅ 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! 🚀