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 👍👍
📍 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 👍👍
👍18❤2