Search This Blog
Do recommend any topic of your choice in the comment section below! Thanks :)
Shahla blogs
- Get link
- X
- Other Apps
Encapsulation in JAVA
Encapsulation in JAVA
What is Encapsulation in JAVA?
Encapsulation
in JAVA:
Encapsulation
is the wrapping of methods and variables/codes and data as a single unit.
We can make a full encapsulated class
in java by making all data members of the class 'private'
We now use the
getter and setter methods to get and set the data respectively.
Example: Java Bean class is an example of a
fully encapsulated class in Java.
Benefits of Encapsulation in JAVA:
Encapsulation
provides control over data like you can make your class either ‘read-only’
or ‘write-only’. You can easily skip any of the two methods: getter or setter.
It is also a
way of hiding data so that other classes are not able to access the data through
private data members.
Encapsulated
class is easier for unit testing.
Important points to remember for
setter methods in encapsulation in java:
- ·
Should
be public in nature
- ·
The
return-type must be ‘void’.
- ·
The
prefix for the setter method is ‘set’.
- ·
It
should be taking some arguments.
Important points to remember for getter
methods in encapsulation in java:
- ·
Should
be public in nature
- ·
It
should have some return-type. It should not be void.
- ·
The
prefix for the getter method is ‘get’.
- ·
It
should not take any argument, unlike the setter method.
- Get link
- X
- Other Apps
Comments
Post a Comment