Java Tut – Encapsulation

Encapsulation is one of the four fundamental OOP concepts.The other three are inheritance,polymorphism and abstraction.

it is a mechanism of wrapping the data variables and code acting on the data together as single unit. In encapsulation teh variables of a class will be hidden from other classes and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
To achieve-

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to modify and view the variables values.

code:
EncapTest.java

the public setXXX() and getXXX() ethods are the access points of the instance variables of the EncapTest class.Normally these methods are referred as getters and setetrs.Therefore any class that wants to access teh variables should access them through these getters and setters.

RunEncap.java

output: