What is encapsulation? Learn encapsulation in JAVA

Home Masters of Computer Science What is encapsulation? Learn encapsulation in JAVA
What is encapsulation? Learn encapsulation in JAVA

Last Updated on by Sumi

Introduction of Encapsulation

  • Besides inheritance, polymorphism and abstraction, encapsulation is one of fundamental OOP concept.
  • mechanism of wrapping up data under single unit
  • this is process ehere code and data are wrapped together in single unit
  • its like a capsule, which consists of several components in medicines
  • Java Bean is fully encapsulated class
  • Variables / data of a class can be hidden from other classes and can only be accessed from member function of its own class.

How to encapsulate in Java?

  • Make variable of class private (Declare variable as private)
  • To modify and view variable values, provide public set and get method.

Benefits of Encapuslation

  • Flexibility: Read-only and write only can be made for fields of the class
  • The class can control what is stored in its field
  • Data Hiding
  • Re-usability