Java Programming
30.7K subscribers
406 photos
203 files
238 links
Everything you need to learn Java Programming

Daily Java tutorials, coding challenges, OOP concepts, DSA in Java & more!
Perfect for beginners, CS students & job seekers.

Downloadable PDFs, cheat sheets, interview prep & projects

For ads: @coderfun
Download Telegram
What is Encapsulation in java ?

📍 Encapsulation is one of the fundamental principle of object oriented programming .
📍 Encapsulation allows to protect the data within a class from outside entities.
📍 Encapsulation helps to achieve hiding the internal information from outside entities.
📍 Data and methods (To access the data) are bundled together within a single unit .(class)
📍 In Java, encapsulation is typically achieved by:
Declaring the class members as private.
Providing public getter and setter methods to access and modify the private attributes.
Note :
Let's say we have 1 private variables in a class

Example - 1:
getter & setter for primitive type private members.
private String empnm;

// get method to access the data
public String GetName() {
return empnm;
}

// set the data
public void setName(String enm) {
this.empnm = enm;
} // call the method & pass the data as param

If we can notice the getter & setter for empnm , below points we can note:
1. getter method will return the variable which is private and it is non parameterized .
2. setter method is a parameterized method which we shall use to set the value for the private member while calling , so only it is parameterized method.

Example -2 :getter & Setter for the private array :

public int[] getarr() {
return arr;
}

// Setter method to modify the private array
public void setarr(int[] brr) {
// You can add validation or other logic if needed
this.arr = brr;
}

Best Programming Resources: https://topmate.io/coding/886839

All the best 👍👍
👍182
13👍1
👍76🔥1
Java Interview Ques & Ans ✔️
👍12🔥1💊1
Java Interview Questions & Answers ✔️
👍7