Scala - Access Modifiers Example

1. Introduction

Access modifiers in Scala control the visibility of classes, objects, methods, and fields to the rest of the program. Understanding and using them correctly is crucial for encapsulating and protecting data and behavior from unintended interference. Scala provides several access modifiers, including private, protected, and public (the default). Additionally, Scala enhances control over access with scoped private/protected levels. This post will discuss and demonstrate each of these modifiers.

Scala - Access Modifiers

Scala's access modifiers include:

- private: Restricts visibility to the containing class or object.

- protected: Allows access to subclasses and restricts access from non-subclass contexts within the same package.

- public: By default, if no modifier is specified, the member is public.

- Scoped Access: Scala allows you to specify access within a certain scope using [this] or a package name [x].

2. Program Steps

1. Define classes and members with various access levels.

2. Attempt to access these members from different scopes to illustrate the effects of the access modifiers.

3. Code Program

class AccessModifierExample {
  private var privateVar = "I am private"
  protected var protectedVar = "I am protected"
  var publicVar = "I am public (default)"

  private[this] var privateThisVar = "I am private to this instance only"
  private[AccessModifierExample] var privateWithinClass = "I am private within class scope"

  def showAccess(): Unit = {
    println(privateVar)
    println(protectedVar)
    println(publicVar)
    println(privateThisVar)
    println(privateWithinClass)
  }
}

object AccessTest extends App {
  val example = new AccessModifierExample()
  // println(example.privateVar) // This will not compile
  // println(example.protectedVar) // This will not compile
  println(example.publicVar) // Accessible

  example.showAccess() // All variables are accessible within their own class
}

class SubclassExample extends AccessModifierExample {
  // println(privateVar) // This will not compile
  println(protectedVar) // Accessible within subclass
  println(publicVar) // Always accessible
}

Output:

I am public (default)
I am private
I am protected
I am public (default)
I am private to this instance only
I am private within class scope
I am protected
I am public (default)

Explanation:

1. privateVar is declared with a private modifier, meaning it is only accessible within the class AccessModifierExample.

2. protectedVar is declared with a protected modifier, which allows access from subclasses like SubclassExample, but not from other contexts.

3. publicVar has no modifier, making it publicly accessible from anywhere.

4. private[this] privateThisVar is a private member specific to the current instance of the class, preventing access even from other instances of the same class.

5. private[AccessModifierExample] privateWithinClass is accessible within the AccessModifierExample class but not from outside it, even in the same package.

6. The AccessTest object tries to access these variables and prints the ones it has access to. It calls the showAccess method to demonstrate that all members are accessible within their defining class.

7. The SubclassExample subclass demonstrates that while it can access the protectedVar and publicVar, it cannot access privateVar from the superclass.


Comments