October 1, 2025

Scala – Access Modifiers

In this lesson we would be covering the three access modifiers in Scala. They are private, protected and default.

For private and protected, objects and classes are labeled with these keywords. But when no specified keyword is used, then the default is public.

  1. Private Modifier
  2. Protected Modifier
  3. Public Modifier
  4. Scope of Protection

 

1. Private Modifier

A private member of a class (method or variable) is visible only within the class or object containing the definition.

The code below shows a private method in a class

class Parent {
  class Child {
    private def show(): Unit = {
      println("I am the child");
    }

    class grandChild {
      show();
    }
  }

  var child1: Child = new Child();
  child1.show(); //Error: the show() method is private
}

In this example, the show method is defined in the Child class and it is private. This means that it can only be access from within the Child class, not from outside. However, the grandChild class is inside the Child class and show the show() method is accessible from there too.

 

2. Protected Modifier

Protected members are accessible from within the class and from classes that are derived from that class.

An example is given below:

class Parent {
  class Child{
    protected def show(): Unit ={
      println("I am the child");
    }
  }

  class Sib extends  Child(){
    show(); // this works fine because Sib extends Class
  }
  
  class Uncle(){
    var child1: Child = new Child();
    child1.show(); // This fails because Uncle does not extend Child
  }
}

The comment explain this. Try to change the Uncle to make it extend Child, then see that it works.

 

3. Public Modifier

This is the default access modifier. For public members, you don’t need to specify the public keyword. Once the keyword is not specified, the the it is public. The code below demonstrates this:

class AccessDemo{  
     var x:Int = 10  
     def show(){  
         println(" x = "+x)  
     }  
}  
  
object MainObject{  
    def main(args:Array[String]){  
        var x = new AccessDemo()  
        x.show()  
    }  
}  

 

4. Scope of Protection

The private and protected access modifiers can be enhanced using qualifiers. This qualifiers are specified in square brackets following the modifier. For example, we can have private[X] or protected[X]. This means that the modifier applies up to X, where X is some enclosing package, class or singleton object.

An example is given below:

package students {
   package teachers {
      class Executive {
         private[students] var workDetails = null

         private[teachers] var friends = null

         private[this] var secrets = null

         def test(another : Executive) {
            println(another.workDetails)
            println(another.secrets) //This fails!!
         }
      }
   }
}

From the code below, we note the following points:

  • the member variable workDetails is accessible to any class within the students package
  • the variable friends is accessible within the teachers package
  • the variable secrets is accessible only on the object within the instance methods (this)

Leave a Reply