Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
192 views

Encapsulation in Java

Encapsulation in Java is a process that wraps code and data together into a single unit. It involves making all data members of a class private and providing public getter and setter methods to access and modify the data. This allows control over how the data is accessed and modified. For example, validation logic can be added to setter methods. The Java Bean class is fully encapsulated with all its data fields made private and accessed through getter/setter methods only. A simple example demonstrates a Student class with a private name field and public getter and setter methods to set and retrieve the name.

Uploaded by

Bezimena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

Encapsulation in Java

Encapsulation in Java is a process that wraps code and data together into a single unit. It involves making all data members of a class private and providing public getter and setter methods to access and modify the data. This allows control over how the data is accessed and modified. For example, validation logic can be added to setter methods. The Java Bean class is fully encapsulated with all its data fields made private and accessed through getter/setter methods only. A simple example demonstrates a Student class with a private name field and public getter and setter methods to set and retrieve the name.

Uploaded by

Bezimena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

javatpoint.

com

http://www.javatpoint.com/encapsulation

Encapsulation in Java
next prev
Encapsulation in java is a process of wrapping code and data together into a single unit , for example capsule
i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data
members of the class private. Now we can use setter and getter methods to
set and get the data in it.
The Java Bean class is the example of fully encapsulated class.

Advantage of Encapsulation in java


By providing only setter or getter method, you can make the class read-only or write-only.
It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only,
you can write the logic inside the setter method.

Simple example of encapsulation in java


Let's see the simple example of encapsulation that has only one field with its setter and getter methods.
1. package com.javatpoint;
2. public class Student{
3. private String name;
4. public String getName(){
5. return name;
6. }
7. public void setName(String name){
8. this.name=name
9. }
10. }
1. package com.javatpoint;
2. class Test{
3. public static void main(String[] args){
4. Student s=new Student();
5. s.setname("vijay");
6. System.out.println(s.getName());
7. }
8. }
Compile By: javac -d . Test.java
Run By: java com.javatpoint.Test
Output: vijay
Next TopicObject class in java
prev next

1/2

2/2

You might also like