Access Modifiers in Scala

Access Modifiers in scala are used to define the access field of members of packages, classes or objects in scala.For using an access modifier, you must include its keyword in the definition of members of package, class or object.These modifiers will restrict accesses to the members to specific regions of code.
There are Three types of access modifiers available in Scala:

  1. Private
  2. Protected
  3. Public

Table:

Modifier Class Companion Subclass Package World
No Modifier/Public Yes Yes Yes Yes Yes
Protected Yes Yes Yes No * No
Private Yes Yes No No * No


What is companion in above table?
It is a singleton object named same as the class.

1. Private: When a member is declared as private, we can only use it inside defining class or through one of its objects.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of private access modifier
class abc
    private var a:Int = 123
    def display()
    {
        a = 8
        println(a)
    }
}
  
object access extends App
{
    // class abc is accessible 
    // because this is in the same enclosing scope
    var e = new abc()
    e.display()
}

chevron_right


Output:

8

Here we declared a variable ‘a’ private and now it can be accessed only inside it’s defining class or through classes object.

2. Protected: They can be only accessible from sub classes of the base class in which the member has been defined.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of protected access modifier
  
class gfg
    // declaration of protected member
    protected var a:Int = 123
    def display()
    {
        a = 8
        println(a)
    }
}
  
// class new1 extends by class gfg
class new1 extends gfg
{
    def display1()
    {
        a = 9
        println(a)
    }
}
  
object access extends App
{
    // class abc is accessible because this
    // is in the same enclosing scope
    var e = new gfg()
    e.display()
    var e1 = new new1()
     e1.display1()
          
}

chevron_right


Output:

8
9

When we extended abc in class new1, protected variable a is now available to be modified cause new1 is a subclass of class abc.

3. Public: There is no public keyword in Scala. The default access level (when no modifier is specified) corresponds to Java’s public access level.We can access these anywhere.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of protected access modifier
  
class gfg
{
    var a:Int = 123
}
object access extends App
{
    var e = new gfg()
    e.a = 444    
    println(e.a)
}

chevron_right


Output:

444


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.