Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Morata, John Adam B.

November 24, 2021


BSIT-2-1 DCIT-50

Please answer the following:

1. What is the formal definition of Encapsulation in Object Oriented Programming using Java?
 In object-oriented computer programming languages, the notion of encapsulation (or
OOP Encapsulation) refers to the bundling of data, along with the methods that operate
on that data, into a single unit. Many programming languages
use encapsulation frequently in the form of classes. Encapsulation may also refer to a
mechanism of restricting the direct access to some components of an object, such that
users cannot access state values for all of the variables of a particular object.
Encapsulation can be used to hide both data members and data functions or methods
associated with an instantiated class or object.

2. What are the Advantages of implementing Encapsulation?


 There are following advantages of encapsulation in Java. They are as follows:
1. The encapsulated code is more flexible and easy to change with new
requirements.
2. It prevents the other classes to access the private fields.
3. Encapsulation allows modifying implemented code without breaking other code
that has implemented the code.
4. It keeps the data and codes safe from external inheritance. Thus, Encapsulation
helps to achieve security.
5. It improves the maintainability of the application.
6. If you don’t define the setter method in the class then the fields can be made
read-only.
7. If you don’t define the getter method in the class then the fields can be made
write-only.

3. How can we achieve Encapsulation in OOP using Java?


 Encapsulation can be achieved by Declaring all the variables in the class as private and
writing public methods in the class to set and get the values of variables

Sources:
https://www.sumologic.com/glossary/ https://www.edureka.co/blog/encapsulation-
encapsulation/ in-java/
https://www.scientecheasy.com/2020/07/ https://www.geeksforgeeks.org/
encapsulation-in-java.html/ encapsulation-in-java/

You might also like