Encapsulation:
*************************************************
*************************************************
Encapsulation
in Java or object oriented programming language is a concept which
enforce protecting variables, functions from outside of class, in order
to better manage that piece of code and having least impact or no impact
on other parts of program due to change in protected code.
Encapsulation
is the technique of making the fields in a class private and providing
access to the fields via public methods. If a field is declared private,
it cannot be accessed by anyone outside the class, thereby hiding the
fields within the class. For this reason, encapsulation is also referred
to as data hiding.
Encapsulation
can be described as a protective barrier that prevents the code and
data being randomly accessed by other code defined outside the class.
Access to the data and code is tightly controlled by an interface.
The
main benefit of encapsulation is the ability to modify our implemented
code without breaking the code of others who use our code. With this
feature Encapsulation gives maintainability, flexibility and
extensibility to our code.
Example:
*************************************************
public class EncapsulationBean{
private String name;
private String id;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getId(){
return id;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setId( String newId){
id = newId;
}
}
*************************************************
In
this class, fields are private so public methods are the access points
to this class's fields from the outside java world. Normally these
methods are referred as getters and setters. Therefore any class that
wants to access the private variables, it should access them through
these getters and setters methods.
*************************************************
public class EncapsulationTest{
public static void main(String args[]){
EncapsulationBean encapBean = new EncapsulationBean();
encapBean.setName("Krishna");
encapBean.setAge(30);
encapBean.setId("12527");
System.out.print("Name:"+encapBean.getName()+"Age:"+
encapBean.getAge());
encapBean.getAge());
}
}
*************************************************
Advantages of Encapsulation:
If
you want maintainability, flexibility, and extensibility (and I guess,
you do), your design must include encapsulation. How do you do that?
- Keep instance variables protected (with an access modifier, mostly private).
- Make public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable.
- For the methods, use the JavaBeans naming convention of set and get.
- Encapsulated Code is more flexible and easy to change with new requirements.
- Encapsulation in Java allows you to control who can access what.
- A class can have total control over what is stored in its fields
*************************************************************
No comments:
Post a Comment