Java OOPC -Encapsulation
Encapsulation
In real life capsule is based for the concept Encapsulation, We use
capsule for the protection and security of the medicine that inside the capsule
from of environmental conditions and
other things because that conditions generated unhealthy components.
In here when it comes to object oriented concept Encapsulation is
that data hiding, protection and security. Further this concept we use for the
variables.
We talk about the GET and SET methods thought the concept
encapsulation,
Getters and setters
For the better security we use access modifier ‘private’ ,because in private modifier only let the
specific class accessing. So private variables can use outside of the specific
method using GET and SET methods.
Indeed we use private keyword for data security purpose but in
some time some specific reasons we need to use those outside of the class we
define it previously, Because of this reason we use getters and setters.
Encapsulation use for the protect the privacy of near data and for the security of those through get set methods for
the purpose of access inside only that
specific method.
Example of the Get Set method:
The `getBalance()` method serves as a getter, allowing
retrieval of the current balance from a class or object, while the
`setNumber()` method functions as a setter, enabling the update or assignment
of a value for a variable representing an account number.
We need Encapsulation in Java in many reasons,
o Getter & Setter
o Security
o Flexibility
Advantages of Encapsulation
1. Encapsulation in Java empowers a class with complete control
over its data members and methods.
2. The class can choose to maintain its data members and methods
as read-only, providing a level of access control.
3. Data hiding.
4. The variables within a class can be set as read-only or
write-only, based on the specific requirements of the programmer.
5. Encapsulation offers the advantage of code reusability,
enhancing the modular and organized design of software.
Through bellow image we can get conclusion about
encapsulation,
Comments
Post a Comment