Wednesday, June 19, 2019

Encapsulation is to make sure that "sensitive" data is hidden from users. To achieve this, you must:

  • declare class variables/attributes as private (only accessible within the same class)
  • provide public setter and getter methods to access and update the value of a private variable

In encapsulation, you never expose your data to an external party. Which makes your application secure.
Java encapsulation is referred as data hiding. But more than data hiding, encapsulation concept is meant for better management or grouping of related data.
With encapsulation, developers can change one part of the code easily without affecting other.

If a data member is declared "private", then it can only be accessed within the same class. No outside class can access data member of that class. If you need to access these variables, you have to use public "getter" and "setter" methods.
The get method returns the variable value, and the set method sets the value.

Example:

public class Person {
  private String name; // private = restricted access
  // Getter  public String getName() {
    return name;
  }

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

public class MyClass {
  public static void main(String[] args) {
    Person myObj = new Person();
    myObj.name = "John";  // error    System.out.println(myObj.name); // error    }
}

Example explained

The get method returns the value of the variable name.
The set method takes a parameter (newName) and assigns it to the name variable. The this keyword is used to refer to the current object.
However, as the name variable is declared as private, we cannot access it from outside this class:


Related Posts:

  • pom.xml in Maven A Project Object Model or POM is the fundamental unit of work in Maven. It contains the project configuration details used by Maven. Some of the configuration that can be specified in the POM are the project dependencies, th… Read More
  • @FindBy, @FindBys, @FindAll, @CacheLookup - Selenium API All are used to mark a field in a page object. @FindBy: It is an alternative mechanism for locating the element or a list of elements. Used in conjunction with PageFactory this allows users to quickly and easily cre… Read More
  • Why we require a separate browser executable file to launch a browser in selenium? It is very frequently asked interview question for me. I was no answer as till now we know only we need drivers( like chromedriver.exe, geckodriver.exe, IEDriverServer.exe) and pass them to the System.setProperty() to… Read More
  • Palindrome Number Program in Java Palindrome Number: A Palindrome number is a number that remains the same when its digits are reversed. We get total 90 numbers between 100 and 1000. public class PalindromeNumber { public static void main(String[] a… Read More
  • Remove Duplicate values from an Array Write a Java Program to remove duplicate values from an array? package seleniumrepo; public class DisticntElements {     public static void printDistinctElements(int[] arr){         for(int i=… Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts