Encapsulation is the whole idea behind encapsulation is to hide the implementation details from the users.
It just like capsule, Its wrapping code and data together into a single unit.
According to figure encapsulation class is just making all the data members of the class private.
It is also known as data hiding.
# Methods of Encapsulation
It just like capsule, Its wrapping code and data together into a single unit.
According to figure encapsulation class is just making all the data members of the class private.
It is also known as data hiding.
# Methods of Encapsulation
- setter( )
- getter( )
1. setter( ) - setter is used for set the data and
2. getter( ) - getter is use for get the data.
For Example:-
**Create a java class Name is EncapsulationExample.java
/**
*
* @author pradeep
*/
public class EncapsulationExample
{
private int spmt;
private String NameOfEmp;
private int AgeOfEmp;
//Use of Getter and Setter methods
public int getEmpSPMT()
{
return spmt;
}
public String getEmpName()
{
return NameOfEmp;
}
public int getEmpAge()
{
return AgeOfEmp;
}
public void setEmpAge(int newValue)
{
AgeOfEmp = newValue;
}
public void setEmpName(String newValue)
{
NameOfEmp = newValue;
}
public void setEmpSPMT(int newValue)
{
spmt = newValue;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~*****^^^^^^^^*****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**Create a java class Name of java class is TestEnsaptulation.java
/**
*
* @author pradeep
*/
public class TestEnsaptulation
{
public static void main(String args[])
{
EncapsulationExample obj = new EncapsulationExample();
obj.setEmpName("monty");
obj.setEmpAge(23);
obj.setEmpSPMT(112233);
System.out.println("Name Of Employee: " + obj.getEmpName());
System.out.println("SPMT Of Employee: " + obj.getEmpSPMT());
System.out.println("Age Of Employee: " + obj.getEmpAge());
}
}
Output : Output of this program:
run:
Name Of Employee: monty
SPMT Of Employee: 112233
Age Of Employee: 23
BUILD SUCCESSFUL (total time: 2 seconds)
No comments:
Post a Comment