Selenium/Java-OOPs-Encapsulation

We have started to learn OOPs concepts from last post, starting with Abstraction. In this post, we will go through the concept of Encapsulation. 

Encapsulation:

  1. Basically, Encapsulation binds data(variables) and code together. It is a process of wrapping up the data in single unit.
  2. We declare our variables as private, so it can not be accessed outside the class. Hence, it is also called data hiding approach.
  3. However, if you wish to access the variables outside the class, we can use public setter and getter methods to access such private variables.


 The first question arises here is, why to declare variables as private if anyway we can use it outside the class using setter and getter methods.. !! That's where the concept of Encapsulation flourishes..!!

Here, we are declaring variables as private and hence one can not get the idea about the implementations of the class. User can only surmise that how the variable will be stored (It can be String, integer or anything.. great, right !?). One more advantage is validations can be performed because of that user can not set any negative or invalid value and we can control our data better.

For example: 


private int validAge;
public void setvalidAge(int age){
      if (age > 18 )
                validAge = age;
}
public int getvalidAge(){
return age;
}

Here, the code will not allow the user to set age below 18, this is one example of validations.

The another advantage of Encapsulation is we can make our data read only or write only. If we initialize the variable first and can only include getter method, the field will become read only.

For Example:


private int validAge=18;
public int getvalidAge(){
return age;
}

Here we have include only getter method and the user can only fetch the value of variable and making the field as read only. Just like this we can make our variable write only by including only setter method.

One another big advantage of Encapsulation is during tracking of your bug. If you declare your variable as public, it can be anywhere in your code but if you make your variable as private, you need to check only one source file. It makes bug tracking easier, reduces the effort and makes unit testing easier for developer.. !!

Encapsulation also makes integration of new requirements facile task as you have declared variables as private and you need to change the code only at one place.

Complete Example:

// Creating class and private variables


public class Encapsulation {

private int id;
private String name;
// getter and setter methods for the variables

public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


// main method

public static void main(String[] args) {
Encapsulation e =new Encapsulation();

e.setId(123);

e.setName("Raj");

System.out.println("Id is: "+e.getId());

System.out.println("Name is: "+e.getName());
}

}

Run the code and observe the results.. !!

Cheers.. !





 
 







Comments

More posts on selenium...

Selenium/Java-OOPs-Abstraction

Selenium: Automate Web Application Login

Selenium/Java- CoC

Selenium/Java-OOPs-Inheritance

Selenium: Waits

Selenium: All about XPath Locator in WebDriver

Selenium: Capturing the screenshot

Selenium: BDD- The Disruptive Framework

Selenium: Frameworks