Skip to main content

Encapsulation

Encapsulation in Java​

Definition​

  • Encapsulation is the process of binding data members with member functions.
  • It provides security and controlled access to data members by restricting direct access.
  • In Java, encapsulation is achieved by declaring data members as private and providing getter and setter methods.

Example 1: Basic Encapsulation​

class Employee{
int empId;
String empName;

void Info(){
System.out.println("Employee ID: " + empId);
System.out.println("Employee Name: " + empName);
}
}

Example 2: Encapsulation with Private Variables​

class Bank{
// Providing security
private int money = 1000; // restrict money variable outside Bank class
}

public class Hero{
public static void main(String[] args) {
Bank bank = new Bank(); // Creating object of Bank class

// System.out.println(bank.money); // Error: Cannot access private variable
}
}

Example 3: Unauthorized Access (Error Demonstration)​

class Bank{
// Providing security
private int money = 1000; // restrict money variable outside Bank class
}

public class Villain{
public static void main(String[] args) {
Bank bank = new Bank(); // Creating object of Bank class

// System.out.println(bank.money); // Error: money has private access in Bank
// bank.money = 0; // Error
}
}

Example 4: Encapsulation with Getter and Setter Methods​

class Bank{
// Providing security
private int money = 1000; // restrict money variable outside Bank class
private int key = 1234;

// Setter Method
public void setHeroMoney(int key, int amount){
if(key == 1234){
money = amount; // Setting money variable
System.out.println("Money set to: " + money); // Printing money variable
}else{
System.out.println("Invalid key!"); // If key is not valid
}
}

// Getter Method
public int getHeroMoney(int key){
if(key == 1234){
return money; // Returning money variable
}else{
System.out.println("Invalid key!"); // If key is not valid
return -1;
}
}
}

public class Villain{
public static void main(String[] args) {
Bank bank = new Bank(); // Creating object of Bank class
int key = 1234; // Creating key variable
int amount = 2000; // Creating amount variable

bank.setHeroMoney(key, amount); // Setting money variable
int money = bank.getHeroMoney(key); // Getting money variable

System.out.println("Money in bank: " + money); // Printing money variable
}
}

Output:​

Money set to: 2000
Money in bank: 2000

Key Points​

  • Encapsulation protects data members by making them private.
  • Getter and setter methods allow controlled access to private data.
  • Direct access to private members is not allowed.
  • Encapsulation improves data security and maintains data integrity.

Encapsulation with Parameterized Constructor in Java​

Definition​

  • Encapsulation protects data members by restricting direct access.
  • Private variables cannot be accessed outside the class.
  • Getter methods allow controlled access to private data.
  • A parameterized constructor initializes object values during creation.

Example: Encapsulated Student Class​

// Class definition
class Student {
// Private data members (Encapsulation)
private int roll;
private String name;
private double marks;

// Parameterized Constructor
public Student(int roll, String name, double marks) {
this.roll = roll;
this.name = name;
this.marks = marks;
}

// Getter methods to access private data members
public int getRoll() {
return roll;
}

public String getName() {
return name;
}

public double getMarks() {
return marks;
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Creating an object using parameterized constructor
Student s1 = new Student(101, "John Doe", 85.5);

// Trying to access private variables directly (will cause an error)
// System.out.println(s1.roll); // Error: roll has private access in Student

// Accessing data using getter methods
System.out.println("Roll Number: " + s1.getRoll());
System.out.println("Name: " + s1.getName());
System.out.println("Marks: " + s1.getMarks());
}
}

Output:​

Roll Number: 101
Name: John Doe
Marks: 85.5

Key Points:​

  • Encapsulation ensures data security by using private access modifiers.
  • Parameterized constructor assigns values to instance variables.
  • Direct access like s1.roll causes an error, ensuring restricted access.
  • Getter methods (getRoll(), getName(), getMarks()) allow controlled data retrieval.

Encapsulation helps in maintaining data integrity and security while providing a clean interface for object manipulation!