Details of encapsulation in Java - Selenium Tutorials Beginners

Java is one of the most widely used programming languages in the world and the major feature that it attracts many software professionals is its user-friendliness. Java is used for writing test scripts and automated tests too. Most of the Selenium users use java because selenium webdriver is also written in java.

This part of our java tutorial covers encapsulation in java. Encapsulation is one of the four fundamental OOPs concepts. Wrapping data and the codes acting on the data together into a single unit is known as encapsulation. The term “Encapsulation” came into existence after the word “Capsule” which contains different medicines in it.

OOPs or object oriented programming concepts are widely used in java to make working with java easier. There are four fundamental OOps concepts in java and they are Encapsulation, Inheritance, Polymorphism and abstraction.

Creation of a fully encapsulated class is possible by making all data members of the class private. Getter and setter methods can be used to get and set the data.

Advantage of Encapsulation in Java

  • The class can be made read only or write only by providing only getter and setter methods.
  • Provides complete control over the data.
  • Implementation code can be changed without affecting the codes of others who are using our codes.
  • Possibility of data hiding.
  • Possibility of reworking on method code at a later stage by hiding the implementation.

Encapsulation can be brought in using different methods

  • Making instance variable protected
  • Creating public accessor methods and using these methods from within the calling code
  • Using getter and setter methods

Example of Encapsulation in Java

Here is a good example for encapsulation in java which has only one field with its getter and setter methods.

  1. //save as Student.java  
  2. package com.javatpoint;  
  3. public class Student{  
  4. private String name;  
  5.   
  6. public String getName(){  
  7. return name;  
  8. }  
  9. public void setName(String name){  
  10. this.name=name  
  11. }  
  12. }  
  13. //save as Test.java  
  14. package com.javatpoint;  
  15. class Test{  
  16. public static void main(String[] args){  
  17. Student s=new Student();  
  18. s.setName(“vijay”);  
  19. System.out.println(s.getName());  
  20. }  
  21. }  

Another example of capsulation in Java

class EmployeeCount
{
  private int NoOfEmployees = 0;
  public void setNoOfEmployees (int count)
  {
      NoOfEmployees = count;
  }
  public double getNoOfEmployees ()
  {
      return NoOfEmployees;
  }
}
class Encapsulation
{
  public static void main(String args[])
  {
     System.out.println(“Starting EmployeeCount…”);
     EmployeeCount employeeCount = new EmployeeCount ();
     employeeCount. setNoOfEmployees (12003);
     System.out.println(“NoOfEmployees = ” + employeeCount. getNoOfEmployees ());
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *