Scala Access Modifiers: Public, Private and Protected Members

1. Access Modifiers in Scala

This article deals with private, protected, and public members of a class in Scala. Let’s begin with the Scala Access Modifiers Tutorial.

Scala Access Modifiers: Public, Private and Protected Members

Scala Access Modifiers: Public, Private and Protected Members

2. Scala Access Modifiers

Constructs like classes, objects, or packages can hold members like variables and methods/functions. We can declare these members to be private or protected. If we don’t, Scala assumes them to be public. But what are these? These are Scala access modifiers; using these, we can restrict the members from being accessible to certain areas of the code. A private modifier to a member means that only the containing class and its objects will be able to access that member. Let’s begin with this one.

Scala Quiz

3. Types of Access Modifiers in Scala

There are 3 types of Scala Access Modifiers:

a. Private Members in Scala

When we declare a member as private, we can only use it inside its defining class or through one of its objects. To declare a member privately, we use the modifier ‘private’:

class Example {
    private var a:Int=7
    def show(){
        a=8
        println(a)
    }
}
object access extends App{
    var e=new Example()
    e.show()
    //e.a=8
    //println(e.a)
}

Output: 8

In this code, we declare the variable ‘a’ to be private. This means that only the class Example can access it. To do this, we use the function show() to modify and access ‘a’. The two lines that we’ve commented try to access ‘a’ outside of example. If we didn’t comment them, we’d get the following two errors:

C:\Users\lifei\Desktop>scalac access.scala

access.scala:10: error: variable a in class Example cannot be accessed in Example

     e.a=8
        ^
access.scala:11: error: variable a in class Example cannot be accessed in Example
       println(e.a)
                 ^

two errors found

b. Protected Members in Scala

We can only access protected members from within a class, from within its immediate subclasses, and from within companion objects. We use the modifier ‘protected’ for this.

Class Example{
    protected var a:Int=7
    def show(){
        a=8
        println(a)
    }
}
class Example1 extends Example{
    def show1(){
        a=9
        println(a)
    }
}
object access extends App{
    var e=new Example()
    e.show()
    var e1=new Example1()
    e1.show1()
    //e.a=10
    //println(e.a)
}

Output:

8
9

Here, because the class Example1 inherits from Example, it can access the variable ‘a’, and also modify it. The purpose for the comments is the same as in the previous case. Let’s see what happens when we modify the code just a bit:

class Example{
    protected var a:Int=7
    def show(){
        println(a)
    }
}
class Example1 extends Example {
    def show1(){
        a=9
        println(a)
    }
}

object access extends App

{
    var e=new Example()
    e.show()
    var e1=new Example1()
    e1.show1()
    //e.a=10
    //println(e.a)
    e1.show()
}

Output:
7
9
9

Here, when we call show() on e1, it calls the inherited show() from Example. This simply prints out the value of ‘a’.

c. Public Members in Scala

All members are default by public. If we do not accompany them with the modifiers ‘private’ or ‘protected’, they’re public. We can access these anywhere.

class Example {
    var a:Int=7
}
object access extends App{
    var e=new Example()
    e.a=8
    println(e.a)
}

This prints 8.

So, this was all about Scala Access Modifiers. Hope you like our explanation.

4. Conclusion

Hence, we studied what is Scala Access Modifiers and its types: a member of a class may be public, private, or protected. For the latter two, we use the modifiers ‘private’ and ‘protected’. Furthermore, if you have any query, feel free to ask in the comment box.

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.