Encapsulation in OOP with Java.

Lotustudy.blog
3 min readOct 26, 2022

--

Encapsulation is a technique for wrapping variables and methods ( code actions or behaviours) together as a single unit. The concept involves (data hiding) which consists of hiding variables of a class from other classes and can be accessed only through the methods of their current class.

Encapsulation in Java is achieved by:

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

Example 1

View and modify a student’s name and age using the encapsulation technique.

Step-by-step explanation :

  • Create a Student.java file and inside of the public class Student, insert the variables such as name(String) and age(integer).
  • The first step is to set the class of the variables as private and then the second step is to insert public setter and getter methods inside the public class Student to enable the user to modify and view the variable name and age values.

After that, we need to save the Student.java file and create a new java file as Main.java inside the same java folder where the Student.java file is located. In the Main.java file, this is where in the main method we need to do two important things. In the first part, we create a new student using new student() and invoke setName and setAge so to insert new values in the main method. For the second part we need to use System.out.println to view the output results which are provided below :

Example 2

View and modify the name, age and numberId of an individual using the encapsulation technique.

Step-step explanation :

  • Create a EncapTest.java file and inside of the EncapTest public class insert the variables name(String), age(integer) and numberId(String).
  • The first step is to set the class of the variables as private and then the second step is to insert the public setter and getter methods inside the public class EncapTest to enable the user to modify and view the variable name, age and numberId.

After that, we need to save the EncapTest.java file and create a new java file as RunEncap.java inside the same java folder where the EncapTest.java file is located. In the RunEncap.java file, this is where in the main method we need to do two important things. In the first part, we create a new encap using new EncapTest() and invoke setName, setAge and setIdNum to insert new values in the main method. For the second part we need to use System.out.println to view the output results which are provided below :

Getter and Setter are methods that act as access points for the instance variables of the EncapTest class. This simply means that any class that wants to access the variables should access them through getters and setters. These types of methods are used to protect our data and make the code more secure.

Benefits of using the Encapsulation technique:

  • The fields of a class can be made read-only or write-only not both at the same time.
  • A class can have total control over what is stored in its fields.

--

--

Lotustudy.blog

IT/Cybersecurity enthusiast who devotes her time writing top-notch high-quality content on a wide range of topics related to IT security and Computer science .