JAVA ENCAPSULATION
ENCAPSULATION
It is one of the four basic OOPS comments in Java. The other three are Inheritance, Polymorphism, and abstraction.
On the connection, class variables can be hidden from other classes, only accessible through the method of their current class.
Therefore it is also known as Data hiding.Java Training Institute in Chennai
Therefore it is also known as Data hiding.Java Training Institute in Chennai
To Be Connected in Java:
2.Provide public setter and getter modes to change the value of the variables and view them.
Simple Example of Encapsulation in Java:
Let see the simple example of the Encapsulation that has only one field with is setter and getter methods:
Encap test variables can be accessed using the following program:
/* Filename : RunEncap.Java*/
Public class RunEncap {
Public static void main (String args[]) {
Encap Test encap = new EncapTest();
encap.setName(“Kumar”);
encap.setAge(22);
encap.setIdnum(“1234na”);
system.out.print(“Name:”+encape.getName()+”Age:”+encap.getAge());
}
}
This will generate the following effect is:
Output
Name: Kumar Age: 20
Benefits of Encapsulation:
1. Fields of a class can only be read or write only.
2. A class can total control over what is saved in its sectors.
3. Since other classes cannot access data through private data members. It is a way to get the data private in Java.
4. The Encapsulation class is easy to test. So it is good for unit testing in the given program
5. The Static IDE’s facilitates the creation of recipients and settlers. So, java is the fastest and easiest to create a linked class.
6. Increased Compliance: Class variables can only be written or read-only to our needs. If we want to read only variables,
we have to set like SetName() and SetAge(). We only need to get rid GetName() and GetAge(). If we want to write only letters
that change.
we have to set like SetName() and SetAge(). We only need to get rid GetName() and GetAge(). If we want to write only letters
that change.
7. Reconstruction: Reuse with the new arrangements increases and makes it easier to change.
Comments
Post a Comment