0% found this document useful (0 votes)
26 views

ENCAPSULATION

Uploaded by

Jenishiya Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

ENCAPSULATION

Uploaded by

Jenishiya Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

ENCAPSULATION

By
Jenishiya M
Definition

 Encapsulation in Java is a process of wrapping code and data


together into a single unit,
for example, a capsule which is mixed of several medicines.
 By encapsulating a class's variables, other classes cannot access them ,
and only the methods of the class can access them.
 Data encapsulation, also known as data hiding.
Advantages of Encapsulation

● By providing only a setter or getter method, you can make the class read-only or write-only. In
other words, you can skip the getter or setter methods
● It provides you the control over the data. Suppose you want to set the value of id which should
be greater than 100 only, you can write the logic inside the setter method. You can write the logic
not to store the negative numbers in the setter methods.
● It is a way to achieve data hiding in Java because other class will not be able to access the data
through the private data members.
● The encapsulate class is easy to test. So, it is better for unit testing.
● The standard IDE's are providing the facility to generate the getters and
setters. So, it is easy and fast to create an encapsulated class in Java.
Private
Keyword

The best use of private keyword is to create a fully encapsulated


class in Java by making all the data members of that class
private.
Getter Method Setter Method
 Getter returns the value
(accessors), it returns the value  While Setter sets or
of data type int, String, double, updates the value
float, etc. (mutators).
 For the program’s convenience,  It sets the value for any
the getter starts with the word variable used in a class’s
“get” followed by the variable programs. and starts with
name. the word “set” followed by
the variable name
To read the
values:-
(i.e)getter
method
Output:
To insert the
values:-
(i.e)setter
method
Output:
THANK
YOU

You might also like