Encapsulation | What is Encapsulation in JAVA | Why we use Encapsulation in our Framework | What is getter & Setter
What is Encapsulation?
Encapsulation is one of the OOPs concept in JAVA
Encapsulation is wrapping of data (Variables) and code (Methods) acting on that data. This helps us protect the data.
In encapsulation the 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.
In Encapsulation we use Getter and Setters. With the help of getter and setters private DataMembers can be access outside the class.
- Setter - This help us set the value for private data member using public setter
- Getter - This help us access the value for private data member using public getter
public class EncapsulationExample {
private String StudentName;
public void setName(String EnterName) {
EnterName=StudentName;
}
public String getName() {
return StudentName;
}
}
class EnterStudentName{
public static void main(String[] args) {
EncapsulationExample E = new EncapsulationExample();
E.setName("Tony Stark");
System.out.println(E.getName());
}
}
How do we use Encapuslation in Framework?
In Framework, when we use POM (Page Object Model) structure, we create different files as below
- Locators
- Actions
- Test
In the locators file we use encapsulation to create private locators and access them using public getters.
Sine encapsulation is use for data security in the framework, creating the locators method in private make in POM structure ensures the stable framework.
Comments
Post a Comment