Skip to main content

Encapsulation in Java

Encapsulation

  • This is one of the interesting concepts. By using this, data will be surrounded by a protective shield and restricts access for this data over other classes. It hides the data sent through it. 
  • This example is the most relative thing for this concept so all people are using this example. Assume a capsule with medicine stuff. It binds all medicines in a single capsule and hides what the medicines are inside. 
  • This can be achieved by declaring a private variable and using the getters and setters method
Declare the variable as private
Get the value for the variable in the setter method
Get the variable value using the getter method

Read and write access

By using this concept we can able to make the system set the value for the private variable and can get the value of the saved variable.

------------------------------------Sample program------------------------------------
package oops;

class ParentClass{
private String temp;
public void getTemp() {
System.out.println("The Value is set in the setter method and the value is ------"+temp+"------");
}
public void setTemp(String temp) {
this.temp = temp;
System.out.println("This is a setter method and ------" + temp + "------ Value is set to the variable temp");
}
}

class TutorialInheritance{
public static void main(String[] args){
ParentClass p = new ParentClass();
p.setTemp("Te-xplore-ch");
p.getTemp();
}
}
------------------------------------------------------------------------------------------------

Output:
This is a setter method and ------Te-xplore-ch------ Value is set to the variable temp
The value is set in the setter method and the value is ------Te-xplore-ch------

Program Explanation

In the above-mentioned program, I have declared one private variable "temp"
To achieve encapsulation I have added the getter and setter method.
In the main method, I set the value of temp as p.setTemp("Te-xplore-ch"); and in the next step I get the saved value using the getter method

Read Access

In this scenario, we can only able to read the saved value of the variable. We can't able too set the value for that

------------------------------------Sample program------------------------------------
package oops;

class ParentClass{
private String temp = "Happy";
public void getTemp() {
System.out.println("The Value is set in the setter method and the value is ------"+temp+"------");
}
}

class TutorialInheritance{
public static void main(String[] args){
ParentClass p = new ParentClass();
p.getTemp();
}
}
------------------------------------------------------------------------------------------------

Output:
The value is set in the setter method and the value is ------Happy------

In the above program, we don't have a setter to set the value as it had a predeclared value for that variable.
So, While calling the variable it sends a value stored in that variable.

Write only access

In this scenario, only the value been set to the variable and can't be able to view the saved value.

------------------------------------Sample program------------------------------------
package oops;

class ParentClass{
private String temp;
public void setTemp(String temp) {
this.temp = temp;
System.out.println("This is a setter method and ------" + temp + "------ Value is set to the variable temp");
}
}

class TutorialInheritance{
public static void main(String[] args){
ParentClass p = new ParentClass();
p.setTemp("Te-xplore-xh");
}
}
-----------------------------------------------------------------------------------------------

Output:
This is a setter method and ------Te-xplore-xh------ Value is set to the variable temp

In this example, we don't have any getter method to get the value stored in that variable

Suggest us to improve ourselves!!

Comments