Skip to main content

Shahla blogs

Blossoming Hope.... In shadows deep, where darkness weaves its tale, A flicker emerges, a whisper, a frail sail. Hope, a luminescent bloom in the night, A beacon that guides with its soft, steadfast light. In the garden of dreams, where doubts may entwine, Hope sprouts like wildflowers, resilient, divine. Petals unfurl with a promise untold, A story of strength, a future to behold. Through tempests that rage, hope stands as a shield, A fortress of solace, a force unrevealed. In the echoes of sorrow, its melody plays, A song of endurance through life's shifting maze. Ephemeral, yet potent, as morning's first ray, Hope dances with dawn, chasing shadows away. Each heartbeat, a rhythm, a testament clear, That in hope's tender embrace, there's nothing to fear.  A phoenix ascending from ashes of plight, Hope paints the horizon in hues pure and bright. With wings of resilience, it soars to the sky, A symbol of courage, refusing to die.  In the tapestry woven with threads of de...

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.

 

Comments

Popular Posts