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

Why We Need Encapsulation?

Encapsulation is needed to minimize potential dependencies and changes that could break code, and is achieved by grouping correlated operations and entities into a single named class, with private fields and public getter and setter methods to access the fields, as demonstrated by an example Student class with private name and no fields accessed via get/set methods.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Why We Need Encapsulation?

Encapsulation is needed to minimize potential dependencies and changes that could break code, and is achieved by grouping correlated operations and entities into a single named class, with private fields and public getter and setter methods to access the fields, as demonstrated by an example Student class with private name and no fields accessed via get/set methods.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Why we need encapsulation?

Encapsulation is needed
1. To minimize the number of potential dependencies with the highest probability of change propagation
2. In order to make changes in your code without breaking the code of all others who use your code

What is encapsulation?
Grouping of correlated operations/entities in a single entity that can be named with a single name. It can also be defined as
putting together all the variables and methods together in a single unit called class. It is the aggregation of data and behavior.

How you will implement encapsulation in java?
Encapsulation in java is achieved by declaring fields in a class as private, while providing access to the fields via public typically
getter and setter methods. Both data and operations on data (methods) are declared in the same class.

Example in notepad
Here methods like get name set name, and data like name, no are declared in the same class student. The data name, no are
declared private. by this information hiding is provided .

You might also like