Scala – Access Modifiers

  • Post category:Scala
  • Reading time:8 mins read

Scala – Access Modifiers

These access modifiers restrict access to the members to certain regions of code.Members of packages, classes or objects can be labeled with the private and protected access modifier , and if we are not using either of these two keywords, then access will be assumed as public.

Types

  • Private
  • Protected
  • Public
  • Object private
  • Package scope

Now lets look into each one of them

Private Member Access

A private member is visible only inside the class or object that contains the member definition.

Example

object PrivateAccess extends App {
   var counter = new Counter()
   counter.display()
}

class Counter {
   private var count= 0 // Private field.

   def display() {
      // Private field is accessible from inside the class.
      count = 100
      println(count)
   }
}

Example

class Bar {
   // Private method of class Foo is not accessible in class Bar.
   // COMPLITATION ERROR : method doSomething in class Foo cannot be accessed in access.Foo
   new Foo().doSomething()
   }

class Foo {
   // Defined a private method.
   private def doSomething() {
      println("f")
   }

   class Inner {
   // Inner class is able to access the private members of class Foo.
   doSomething()  
   }
}

Protected Member Access

A protected member is only accessible from subclasses of the class in which the member is defined.

Following is the example code snippet to explain protected member −

Example

class Parent {
   // Defining a protected member
   protected var parentCount = 100
   def displayCount() {
      parentCount = 0
      println(parentCount)
   }
}

class Child extends Parent {
   def displayCountChild() {
      // Child class is able to access the private member of it Parent class.
      parentCount = 1000
      println(parentCount)
   }
}

object access extends App {
   var parent = new Parent()
   parent.displayCount()  
  
   var child = new Child()
   child.displayCountChild()
}

Public Member Access

Unlike private and protected members, it is not required to specify Public keyword for Public members. There is no explicit modifier for public members. Such members can be accessed from anywhere.

Example

object PublicAccess extends App{
   var demo = new Demo()
   // Able to access the public member of class Demo. 
   demo.count = 444 
   println(demo.count)
}

class Demo{
   // There is no explicit modifier for public members.
   var count = 1000
}

Example

class Parent {
   // Defining a public member
   // There is no explicit modifier for public members.
   var parentCount = 100

   def displayCount() {
      parentCount = 0
      println(parentCount)
   }
}

class Child extends Parent {
   def displayCountChild() {
      // Child class is able to access the public member of it Parent class.
      parentCount = 1000
      println(parentCount)
   }
}

object access extends App {
   var parent = new Parent()
   parent.displayCount()

   var child = new Child()
   child.displayCountChild()
}

Object-private scope

The most restrictive access is to mark a method as “object-private.” When you do this, the method is available only to the current instance of the current object. Other instances of the same class cannot access the method.

You mark a method as object-private by placing the access modifier private[this] before the method declaration:

Example

object ObjectPrivate extends App{
   var f = new Demo()
   var f1 = new Demo()
   f.doSomething(f1)
}

class Demo {
   // Marking method as object-private by placing the access modifier private[this] before the method.
   private[this] def isThisInstance = true

   def doSomething(other: Demo){
      // This will print true.
      println(this.isThisInstance)

      // COMPLIATION ERROR
      println(other.isThisInstance) }
}

Explanation− In the following example, the method doSomething takes an instance of a Demoobject, but because the doSomethingmethod is declared as an object-private method, the code won’t compile. The code won’t compile because the current Demo instance can’t access the doSomethingmethod of the other instance, because doSomethingis declared as private[this].

Package Level Scope

Package scope access is used to make a method available to all members of the current package, what would be called “package scope” in Java . Mark the method as being private to the current package with the private[packageName] syntax.

In the following example, the method doX can be accessed by other classes in the same package (the model package), but the method doY is available only to the Foo class:

Example

package co.proedu.user {
   class User {
      // Using package scope with method doSomething()
      private[user] def doSomething() {}
      private def doStuff() {}
   }
   
   class Subscriber {
      val subscriber = new User
      // Below expression will compile even though the method is private. This is because 
      // doSomething method has package level access. 
      subscriber.doSomething() 

      // COMPILATION ERROR. doStuff is private. 
      subscriber.doStuff()
   }
}

Summary

The Scala approach to access modifiers is different than Java. Though it offers more power than Java, it’s also a little more complicated.

Access modifierDescription
private[this]The method is available only to the current instance of the class it’s declared in.
privateThe method is available to the current instance and other instances of the class it’s declared in.
protectedThe method is available only to instances of the current class and subclasses of the current class.
private[user]The method is available to all classes beneath the co.proedu.user package.
No modifierThe method is public.
Descriptions of Scala’s access control modifiers