Sitemap
Sithila Sihan Somaratne

Sithila Sihan Somaratne

I'm a student. I love programming and I would like to become a Senior Software Engineer.

Java — Encapsutation

2 min readOct 17, 2024

--

Note: I am so sorry! It happened again. It nearly passed a month since I didn’t touch Medium…. so, I didn’t write any blogs since then, but here I am back now!

What is Encapsulation?

The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:

  • declare class variables/attributes as private
  • provide public get and set methods to access and update the value of a private variable

Getters and Setters

You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods. In programming, we call them “getters” and “setters”.

The get method returns the variable value, and the set method sets the value.

Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case:

public class Person {
private String name; // private = restricted access

// Getter
public String getName() {
return name;
}

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

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.

Get Sithila Sihan Somaratne’s stories in your inbox

Join Medium for free to get updates from this writer.

However, as the name variable is declared as private, we cannot access it from outside this class:

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

If the variable was declared as public, we would expect the following output:

Bill

However, as we try to access a private variable, we get an error:

MyClass.java:4: error: name has private access in Person
myObj.name = "Name";
^
MyClass.java:5: error: name has private access in Person
System.out.println(myObj.name);
^
2 errors

Instead, we use the getName() and setName() methods to access and update the variable:

public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("Bill"); // Set the value of the name variable to "John"
System.out.println(myObj.getName());
}
}

// Outputs "Bill"

Why we should use Encapsulation?

Because of some few reasons:

  • Better control of class attributes and methods
  • Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
  • Flexible: the programmer can change one part of the code without affecting other parts
  • Increased security of data

--

--

Sithila Sihan Somaratne
Sithila Sihan Somaratne