In last tutorial, we saw how RecyclerView is implemented in Android. Now, I'm gonna to explain what is encapsulation and what's the use of using it etc.
Encapsulation is one of the OOPS concepts in Java. Encapsulation allows the developers to hide the implementation from the user. To encapsulate, you've to change the data members ie variables into private modifier to prevent access from other classes. So that no other classes can access these private data members(variables). If you want to access or store any values inside the variable you can access it by way of calling the getter and setter methods of the class.
Real World Example : Consider a scenario, you're setting a password in your Mac or PC to protect some important datas. Do you know how the passwords are stored inside ? The answer is NO. But we're just setting the password that's all but we don't know where it's stored, how much space it needed to store and so on. So what I'm trying to say is the implementation of storing the password is hidden from user. This is encapsulation.
By this way you can provide any logic with the getter and setter methods.
Ex : Read Only and Write Only to control the datas.
Encapsulation is one of the OOPS concepts in Java. Encapsulation allows the developers to hide the implementation from the user. To encapsulate, you've to change the data members ie variables into private modifier to prevent access from other classes. So that no other classes can access these private data members(variables). If you want to access or store any values inside the variable you can access it by way of calling the getter and setter methods of the class.
Real World Example : Consider a scenario, you're setting a password in your Mac or PC to protect some important datas. Do you know how the passwords are stored inside ? The answer is NO. But we're just setting the password that's all but we don't know where it's stored, how much space it needed to store and so on. So what I'm trying to say is the implementation of storing the password is hidden from user. This is encapsulation.
package in.coderearth; public class DeveloperEncapDemo{ //private data members private String devName; private int devAge; private int devExpYear; //Getter methods public String getDevName(){ return devName; } public int getDevAge(){ return devAge; } public int getDevExperienceYear(){ return devExpYear; } //Setter methods public void setDevName(String devNameVal){ this.devName = devNameVal; } public void setDevAge(int devAgeVal){ this.devAge = devAgeVal; } public void setDevExpYear(int devExpYearVal){ this.devExpYear = devExpYearVal; } //Getter methods public String getDevName(){ return devName; } public int getDevAge(){ return devAge; } public int getDevExpYear(){ return devExpYear; } // Other class public class EncapsulationDevDemo{ public static void main(String args[]){ DeveloperEncapDemo devObj = new DeveloperEncapDemo(); //setting values devObj.setDevName("Vino"); devObj.setDevAge(21); devObj.setDevExpYear(2); //getting values System.out.println("Developer Name : "+devObj.getDevName()); System.out.println("Developer Age : "+devObj.getDevAge()); System.out.println("Developer Experience Year : "+devObj.getDevExpYear()); } }
Ex : Read Only and Write Only to control the datas.
Nice Post. Keep posting. msbi online training
ReplyDeleteosb training