Methods, variables, constructors, and structures that are declared private can only be accessed within the declaring class. This is with the exception of private top-level functions and properties that are visible to all members of the same file. Private variables within a class can be accessed from outside the class be declaring getter and setter methods that permit access. Defining setter and getter methods in Java is shown in the following code:
public class Person {
Person(String fullName, int age) {
this.fullName = fullName;
this.age = age;
}
private String fullName;
private int age;
public String getFullName() {
return fullName;
}
public int getAge() {
return age;
}
}
In Kotlin, setter and getter creation is as follows:
public class Person(private var fullName: String) {
var name: String
get() = fullName
set(value) {
fullName = value
}
}
Using the private access modifier is the main means of information hiding within programs. Information hiding is also known as encapsulation.