OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 5 : Accessing Private Members of a Class in Java

 

Unit - 2

Topic - 5 : Accessing Private Members of a Class in Java

In Java, private members cannot be accessed directly from outside the class.
This is part of encapsulation, where we hide the internal details and expose only the necessary methods to interact with the data.


Why Private Members?

  • They protect the internal state of the object from being modified directly.

  • They help maintain data integrity.

  • They allow controlled access through methods (also called getters and setters).


Example: Using Getters and Setters to Access Private Data

class Test { int a; // default access public int b; // public access private int c; // private access // Setter method for c void setc(int i) { c = i; } // Getter method for c int getc() { return c; } } public class Main { public static void main(String[] args) { Test ob = new Test(); ob.a = 1; // OK - default access within same package ob.b = 2; // OK - public access // ❌ Not OK - private member cannot be accessed directly // ob.c = 100; // Error: c has private access in Test // ✅ Access through methods ob.setc(100); // Display values System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc()); } }

Sample Output

a, b, and c: 1 2 100

Explanation

  1. a – has default (package-private) access, so it can be accessed from the Main class since both are in the same package.

  2. b – is public, so it can be accessed from anywhere.

  3. c – is private, so it can only be accessed inside the Test class.

  4. The methods setc() and getc() are public methods that allow controlled access to c.


Key Points

  • private members are hidden from outside classes.

  • getter methods return the value of private variables.

  • setter methods modify the value of private variables.

  • This approach is known as data hiding and is a key principle of Object-Oriented Programming (OOP).

Example: Accessing Private Data with Getters and Setters

class Employee { private String name; // private member private double salary; // private member // Setter for name public void setName(String n) { name = n; } // Getter for name public String getName() { return name; } // Setter for salary public void setSalary(double s) { if (s > 0) { // validation check salary = s; } else { System.out.println("Invalid salary!"); } } // Getter for salary public double getSalary() { return salary; } } public class EmployeeTest { public static void main(String[] args) { Employee emp = new Employee(); // Set values using setters emp.setName("John Doe"); emp.setSalary(50000); // Get and display values using getters System.out.println("Employee Name: " + emp.getName()); System.out.println("Employee Salary: " + emp.getSalary()); // Try setting an invalid salary emp.setSalary(-1000); } }

Sample Output

Employee Name: John Doe Employee Salary: 50000.0 Invalid salary!

Explanation

  • The variables name and salary are private, so they cannot be accessed directly from EmployeeTest.

  • Getter methods (getName(), getSalary()) are used to retrieve values.

  • Setter methods (setName(), setSalary()) are used to update values with an optional validation check (e.g., salary must be positive).

  • If invalid data is passed, the setter can reject it and show a message.

Comments

Popular posts from this blog

Career Guide - B.Tech Students

Artificial Intelligence - UNIT - 1 Topic - 1 : Introduction to AI (Artificial Intelligence)

Financial Aid for Students: Scholarships from Government, NGOs & Companies