Encapsulation and Access Modifiers

Encapsulation protects data using access modifiers: private, protected, and public (default).

Private Members

class Secret {
  private val key = "1234"
  def reveal(): Unit = println(s"The key is $key")
}

Use getter/setter methods or val/var properties for controlled access.

← PrevNext →