What is Encapsulation in Java?
Encapsulation defines as the wrapping up of data
into a single unit. It is the mechanism that binds code and data together which
it manipulates. Also we can think about encapsulation as it a
protective shield that protects the data from being accessed by the outside of
this protected area.
In encapsulation
variables or data of a class is hidden from any other class and can be accessed
only through any member functions of own class in which they are declared.
In encapsulation
data is hidden from other classes, so it is also known as data-hiding.
In java encapsulation
can be achieved by making all the variables private and by writing public
methods in the class to get and set the values of variables which is declared
as private.
Java Bean class
the best example of a fully encapsulated class.
What is Advantages of Encapsulation in Java?
Data Hiding: The user will have no idea about inner implementation of the
class and it will not be visible to users that how the class is storing the
values in the variables. They only knows only we are passing the values to
setter methods and variables get initialized with that values.
Increase flexibility: We can make the variables of the
class as read only or write only depend on requirement. We can make variables
read only by omit setter methods, if we want to make variables write-only then
we need to omit getter methods.
Re-usability: Encapsulation also improves the reusability and easy to
change with new requirements.
Testing code is easy: Encapsulated code is easy to test for
unit testing.
Other
advantages it provide the control over the data, suppose we want to set the
value of id which should be greater than 100 only we can write the login inside
the setter method.
A simple example of program to demonstrate encapsulation
public class MyClass {
//here variables are declared as private and
//these variables can only be accessed by public methods of
class
private String name;
private int passport_no;
private int age;
//get method
for name to access
public String getName() {
return name;
}
//set method for name to access
public void setName(String name) {
this.name = name;
}
//get method for passport_no to access
public int getPassport_no() {
return passport_no;
}
//set method for passport_no to access
public void setPassport_no(int passport_no) {
this.passport_no = passport_no;
}
//get method for age to access
public int getAge() {
return age;
}
//set method for age to access
public void setAge(int age) {
this.age = age;
}
}
In the above
example in MyClass all the variables i.e., name,passpoer_no and age are
declared as private to achieve encapsulation. The get methods like
getName(),getPassport_no() and getAge()
are aet as public ans these methods are used to access private variables. The
set methods like setName(), setPassport_no() and setAge() are also public and
used to access these variables to set the values of the variables.
Now the program to access the variables of the MyClass class
is shown below.
public class TestMain {
public static void main(String[] args) {
MyClass obj=new MyClass();
//setting the
values to variables
obj.setName("'The Tech Matin'");
obj.setPassport_no(123);
obj.setAge(23);
//getting
the values
String name=obj.getName();
int passport_no=obj.getPassport_no();
int age=obj.getAge();
System.out.println(name+" "+passport_no+" "+age);
//obj.name;//error
//if try to direct access to name then we will get compile time error
saying
//The field MyClass.name is not visible
}
}
Output:
'The Tech Matin' 123 23
Example of Read
Only Class
public class MyClass{
//private college name
private String college="Elphinstone College";
//getter method to access
public String getCollege() {
return college;
}
}
Now we can not change the
value of college.
Example of Write
Only Class
public class MyClass{
//private college name
private String college="Elphinstone College";
//setter method
public void
setCollege(String college) {
this.college = college;
}
}
Now we can not get the
value of college.
See Also
No comments