Encapsulation in Java (Getters & Setters Explained)

(Based on your video content)


๐Ÿš€ Introduction

In Object-Oriented Programming (OOP), one of the most important concepts is:

๐Ÿ‘‰ Encapsulation

Encapsulation helps you:

  • Protect your data
  • Control access to variables
  • Write safer and more reliable code

In this article, we will understand encapsulation with simple examples and practical code.


โŒ The Problem: Direct Access to Data

In earlier programs, we used code like:

s1.name = "Alice";
s1.age = 20;

๐Ÿ‘‰ This means:

  • Anyone can directly change your data
  • No control over values

๐Ÿšจ What Can Go Wrong?

s1.marks = -500;

๐Ÿ‘‰ This is invalid data, but Java allows it if variables are public.


โš ๏ธ Issues with Public Variables

  • No validation
  • Anyone can modify data
  • Data becomes unsafe
  • Bugs become hard to trace
  • Security is compromised

โœ… Solution: Encapsulation

๐Ÿ‘‰ Encapsulation means:

  • Wrapping data + methods into one unit
  • Restricting direct access to data
  • Allowing access only through controlled methods

๐Ÿง  Definition

Encapsulation is:

๐Ÿ‘‰ Data hiding + controlled access


๐Ÿ” Step 1: Make Variables Private

class Student {
private String name;
private int age;
private int marks;
}

๐Ÿ’ก What Happens?

  • Variables are hidden
  • Cannot be accessed directly
s1.marks = 90; // โŒ ERROR

๐Ÿ”‘ Step 2: Use Getters and Setters


๐Ÿงฉ What are Getters?

  • Used to read data
public int getMarks() {
return marks;
}

๐Ÿงฉ What are Setters?

  • Used to update data
public void setMarks(int m) {
marks = m;
}

๐Ÿ” Adding Validation (Very Important)

public void setMarks(int m) {
if (m >= 0 && m <= 100) {
marks = m;
}
}

๐Ÿ’ก Why This Matters?

  • Prevents invalid values
  • Protects object data

๐Ÿ‘‰ Example:

s1.setMarks(-500); // โŒ Ignored
s1.setMarks(80); // โœ”๏ธ Accepted

๐Ÿ—๏ธ Complete Example


๐Ÿ“˜ Student Class

class Student {
private String name;
private int age;
private int marks; // Setter Methods
public void setName(String n) {
name = n;
} public void setAge(int a) {
age = a;
} public void setMarks(int m) {
if (m >= 0 && m <= 100) {
marks = m;
}
} // Getter Methods
public String getName() {
return name;
} public int getAge() {
return age;
} public int getMarks() {
return marks;
}
}

๐Ÿ–ฅ๏ธ Main Class

class Main {
public static void main(String[] args) { Student s1 = new Student(); s1.setName("Ajay");
s1.setAge(25);
s1.setMarks(80); System.out.println(s1.getMarks());
}
}

๐Ÿ–จ๏ธ Output

80

๐Ÿ” Before vs After Encapsulation

Without EncapsulationWith Encapsulation
Direct access โŒControlled access โœ”๏ธ
No validation โŒValidation โœ”๏ธ
Unsafe data โŒSafe data โœ”๏ธ
Hard to debug โŒEasy to manage โœ”๏ธ

โš ๏ธ Common Mistakes


โŒ No Validation in Setter

public void setMarks(int m) {
marks = m; // โŒ unsafe
}

โŒ Making Variables Public

public int marks; // โŒ wrong approach

โŒ Changing Data Directly

s1.marks = -10; // โŒ not allowed

๐Ÿ”ฅ Key Takeaways

  • Encapsulation = Data hiding + controlled access
  • Use private variables
  • Use getters and setters
  • Always validate input
  • Prevent invalid data

๐Ÿง  Final Insight

๐Ÿ‘‰ Without encapsulation:

  • Data = unsafe โŒ

๐Ÿ‘‰ With encapsulation:

  • Data = protected, controlled, reliable โœ…

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *