Encapsulation

Some data within a class might be considered private implementation details that should not be exposed to anything outside the class. Other data might need to be available, but its access should be controlled. Encapsulation refers to hiding data from outside entities. By carefully considering which data to hide, and which data to expose, class dependencies can be minimized, controlled, and anticipated, which makes refactoring and extension easier.

In the following example, the logic for how the name should be formatted is encapsulated within the getFormattedName method:

class Person(val firstName: String, val lastName: String) {
fun getFormattedName() = "$lastName, $firstName"
}

This allows for the logic to be changed without affecting callers of the method. The change would be isolated to, and encapsulated in, the Person class.