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:

  • TestNG Annotations What is TesNG and What are the annotations in the TestNG?      Ans: TestNG is testing framework to run the test cases. TestNG supports annotations like:       @BeforeSuite       &… Read More
  • Hashmap vs Hashtable Difference between Hashmap and Hashtable? HashMap is non-synchronized. Hashtable is synchronized.  HashMap allows multiple threads where as hashtable doesn't HashMap is not thread-safe where as hashtable is. HashMap … Read More
  • Static Keyword Static is a keyword in Java, which is used for static blocks variables methods and  inner classes or nested classes A static member can be accessed directly with the class name with out creating an object.  Th… Read More
  • HashMap vs ConcurrentHashMap Difference between HashMap and ConcurrentHashMap: ConcurrentHashMap is thread-safe that is the code can be accessed by single thread at a time. while HashMap is not thread-safe HashMap can be synchronized by using sy… Read More
  • String Reverse Example public class StringReverse { public static void main(String[] args) { String str="selenium",reverse=""; for(int i=str.length()-1;i>=0;i--) { reverse=reverse+str.charAt(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