Encapsulation in OOP
Encapsulation in OOP
🛠 Java-তে Encapsulation:
1. Variable গুলো private করি (যাতে বাইরের কেউ সরাসরি access/change করতে না পারে)।
2. public getter & setter method তৈরি করি (যাতে নিয়ন্ত্রিতভাবে data access/change করা যায়)।
🔹 Code Example:
java Copy Edit
class Person { private String name; // Data hide করা হয়েছে (Encapsulation) // Setter
method (Value set করার জন্য) public void setName(String newName) { name = newName; } //
Getter method (Value read করার জন্য) public String getName() { return name; } } public
class Main { public static void main(String[] args) { Person p1 = new Person();
p1.setName("Rahim"); // Data assign করা হচ্ছে System.out.println(p1.getName()); // Output:
Rahim } }
🔥 সুবিধা:
✅ Data Security: বাহির থেকে সরাসরি পরিবর্তন করা যায় না।
✅ Controlled Access: getter-setter দিয়ে নিয়ন্ত্রণ করা যায়।
✅ Code Maintainability: পরিবর্তন সহজ হয়।
🔹 সহজ ভাষায়:
Encapsulation হলো class-এর ভেতরে data লুকিয়ে রাখা, এবং নির্দিষ্ট method দিয়ে সেটি নিয়ন্ত্রিতভাবে
ব্যবহার করা। 🚀
in english
⭐ Easy Explanation:
Think of a medicine capsule. Inside the capsule, there are different ingredients, but people cannot
access or change them directly. Similarly, in Encapsulation, the internal data of a class cannot be
accessed directly from outside.
🔹 Example in Java:
java Copy Edit
class Person { private String name; // Data is hidden (Encapsulation) // Setter method (To
set value) public void setName(String newName) { name = newName; } // Getter method (To get
value) public String getName() { return name; } } public class Main { public static void
main(String[] args) { Person p1 = new Person(); p1.setName("Rahim"); // Assigning value
System.out.println(p1.getName()); // Output: Rahim } }
🔥 Advantages of Encapsulation:
✅ Data Security – Prevents direct modification of data.
✅ Controlled Access – Access is managed through getter and setter methods.
✅ Code Maintainability – Easy to modify and maintain the code.
🔹 In Simple Words:
Encapsulation means hiding the data inside a class and controlling access using methods. 🚀
👉 Key Features:
✅ Focuses on functions (procedures).
✅ Data and functions are separate.
✅ Uses a top-down approach.
✅ Less secure because data is accessible globally.
✅ Examples: C, Pascal, BASIC
👉 Example in C:
c Copy Edit
#include <stdio.h> // Function to print details void displayDetails(char name[], int age) {
printf("Name: %s\n", name); printf("Age: %d\n", age); } int main() { char name[] = "Rahim";
int age = 25; displayDetails(name, age); // Calling function return 0; }
👉 Key Features:
✅ Focuses on objects.
✅ Uses Encapsulation, Inheritance, Polymorphism, Abstraction.
✅ Follows a bottom-up approach.
✅ More secure because data is hidden inside objects.
✅ Examples: Java, C++, Python
👉 Example in Java:
java Copy Edit
class Person { private String name; // Encapsulated data private int age; // Constructor
public Person(String name, int age) { this.name = name; this.age = age; } // Method to
display details public void displayDetails() { System.out.println("Name: " + name);
System.out.println("Age: " + age); } } public class Main { public static void main(String[]
args) { Person p1 = new Person("Rahim", 25); p1.displayDetails(); // Calling method } }
🔹 Key Differences
Feature Procedural Programming Object-Oriented Programming (OOP)
Data Handling Data and functions are separate Data and functions are inside objects
Security Less secure (global data access) More secure (data encapsulation)
Code Reusability Low (no inheritance) High (uses inheritance & polymorphism)
Use OOP when building large, scalable, and maintainable applications (like Java for enterprise
applications).
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
public class WrapperExample { public static void main(String[] args) { int num = 10; //
Primitive type Integer obj = Integer.valueOf(num); // Wrapping (Boxing) int newNum =
obj.intValue(); // Unwrapping (Unboxing) System.out.println("Wrapped Integer: " + obj);
System.out.println("Unwrapped Integer: " + newNum); } }
✅ Output:
sql Copy Edit
Wrapped Integer: 10
Unwrapped Integer: 10
public class AutoBoxingExample { public static void main(String[] args) { Integer obj = 20;
// Autoboxing (int → Integer) int num = obj; // Unboxing (Integer → int)
System.out.println("Autoboxed: " + obj); System.out.println("Unboxed: " + num); } }
✅ Output:
makefile Copy Edit
Autoboxed: 20
Unboxed: 20
✅ Output:
arduino Copy Edit
🔹 Summary
✔ Wrapper classes allow primitive types to be used as objects.
✔ Autoboxing & Unboxing simplify conversions.
✔ Wrapper classes provide useful methods like parseInt() , toString() , and MAX_VALUE .
✔ Useful in collections, multithreading, and object-oriented programming. 🚀
2. Utility Methods – Wrapper classes provide methods to convert values ( parseInt() , toString() ).
3. Autoboxing & Unboxing – Java automatically converts between primitive types and objects.
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
public class WrapperExample { public static void main(String[] args) { int num = 10; //
Wrapping (Boxing) Integer obj = Integer.valueOf(num); // Unwrapping (Unboxing) int newNum =
obj.intValue(); System.out.println("Wrapped Integer: " + obj);
System.out.println("Unwrapped Integer: " + newNum); } }
✅ Output:
sql Copy Edit
Wrapped Integer: 10
Unwrapped Integer: 10
public class AutoBoxingExample { public static void main(String[] args) { Integer obj = 20;
// Autoboxing (int → Integer) int num = obj; // Unboxing (Integer → int)
System.out.println("Autoboxed: " + obj); System.out.println("Unboxed: " + num); } }
✅ Output:
makefile Copy Edit
Autoboxed: 20
Unboxed: 20
✅ Output:
arduino Copy Edit
🔹 Summary
✔ Wrapper classes allow primitive types to be used as objects.
✔ Autoboxing & Unboxing simplify conversions.
✔ Wrapper classes provide useful methods like parseInt() , toString() , and MAX_VALUE .
c. Integer i = Integer.valueOf("23");
✅ Valid (Compiles)
👉 Integer.valueOf("23") converts the string "23" into an Integer object.
✅ Valid (Compiles)
👉 Integer.parseInt("23", 8) converts "23" from octal (base 8) to decimal ( 2×8 + 3 = 19 ).
f. Double d = Double.valueOf("23.45");
✅ Valid (Compiles)
👉 Double.valueOf("23.45") converts the string "23.45" into a Double object.
g. int i = (Integer.valueOf("23")).intValue();
✅ Valid (Compiles)
👉 Integer.valueOf("23") creates an Integer object, then .intValue() extracts the int value.
✔ Equivalent:
java Copy Edit
h. double d = (Double.valueOf("23.4")).doubleValue();
✅ Valid (Compiles)
👉 Double.valueOf("23.4") creates a Double object, then .doubleValue() extracts the double value.
✔ Equivalent:
java Copy Edit
i. int i = (Double.valueOf("23.4")).intValue();
✅ Valid (Compiles)
👉 Double.valueOf("23.4") creates a Double object,
then .intValue() converts 23.4 to 23 (truncates decimal).
✔ Equivalent:
java Copy Edit
j. String s = (Double.valueOf("23.4")).toString();
✅ Valid (Compiles)
👉 Double.valueOf("23.4") creates a Double object, then .toString() converts it to "23.4" .
✔ Equivalent:
java Copy Edit
String s = String.valueOf(23.4);
✔ Example:
java Copy Edit
✅ Output:
arduino Copy Edit
✔ Example:
java Copy Edit
✅ Output:
sql Copy Edit
✔ Example:
java Copy Edit
✅ Output:
arduino Copy Edit
✔ Example:
java Copy Edit
🔹 Summary Table
Conversion Method 1 Method 2
✅ Best Practice: Use parseInt() and parseDouble() for conversions from strings to numbers. 🚀
Given Code:
public class Test { public static void main(String[] args) { Integer x = new Integer(3);
System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } }
Step-by-Step Execution:
1. Integer x = new Integer(3);
Note: new Integer(int) is deprecated in Java 9+. The recommended approach is Integer x
= Integer.valueOf(3); .
2. System.out.println(x.intValue());
Output: 3
3. System.out.println(x.compareTo(new Integer(4)));
Returns 0 if x == 4
Returns 1 if x > 4
Output:
diff Copy Edit
3
-1