Controlling Method Scope In Scala - GeeksforGeeks

06 Nov, 2019
As the name suggests Access Modifiers in scala helps to restrict the scope of a class, variable, method or data member. Controlling Method Scope In Scala helps to restrict the scope of method or data member. There are five types of controlling method scope in Scala:
Public ScopePrivate ScopeProtected ScopeObject-private ScopePackage Specific
Public Scope
Private Scope
Protected Scope
Object-private Scope
Package Specific
When no access modifier is specified for a class, method or data member, it is said to be having the default access modifier by default.
The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible anywhere using package & imports or by creating new instances.Example :// Scala program of Public Scope// package testAclass classA{    def method1(): Unit=    {        println("method1")    }}  // Creating objectobject GfG{     // Main method    def main(args: Array[String])    {        // classA in the same package         // as the main method         var x = new classA        x.method1()    }}Output :method1
Example :
// Scala program of Public Scope// package testAclass classA{    def method1(): Unit=    {        println("method1")    }}  // Creating objectobject GfG{     // Main method    def main(args: Array[String])    {        // classA in the same package         // as the main method         var x = new classA        x.method1()    }}
Output :
method1

Example :
Private modifier is same as private in java. By marking a method or variable private it is available to the current class and its members and any of instances of the same class.
Any other object/class of same package will not be able to access the private members.
This is done by using the private access modifier.Example :// Scala program of Private Scope// package testAclass classA {    var x = 1    private def method1: Unit =    {        println("method1")    }}  // Creating objectobject GfG{    // Main method    def main(arg: Array[String])    {        var obj1 = new classA        printf("x = "+obj1.x)        // println(obj1.method1) error: method         // method1 in class classA cannot         // be accessed in classA    }}Output:x = 1

Example :
// Scala program of Private Scope// package testAclass classA {    var x = 1    private def method1: Unit =    {        println("method1")    }}  // Creating objectobject GfG{    // Main method    def main(arg: Array[String])    {        var obj1 = new classA        printf("x = "+obj1.x)        // println(obj1.method1) error: method         // method1 in class classA cannot         // be accessed in classA    }}
Output:
x = 1

Scala protected is different from protected in java. To mark a member protected, use the keyword protected before a class or variable.
Protected members can be accessed only by the sub classes in the same package.Example :// Scala program of Protected Scope// package testclass classab{    protected var ab: Int=4    var ad: Int =1}  // Creating objectobject GfG extends classab{     // sub class    // Main method    def main(args: Array[String])    {        println(ab) //can be accessed        println(ad) //can be accessed    }}Output:4
1

// Scala program of Protected Scope// package testclass classab{    protected var ab: Int=4    var ad: Int =1}  // Creating objectobject GfG extends classab{     // sub class    // Main method    def main(args: Array[String])    {        println(ab) //can be accessed        println(ad) //can be accessed    }}
Output:
4
1

Protected members cannot be accessed by other members in other packages even with imports.Example :// Scala program of Protected Scope// package testApackage testA{    class classA     {        protected var ab: Int=4        var ad: Int =1    }}  // another package testBpackage testB{    // importing all the members     // from testA package    import testA._          // Creating object    object GfG    {        // Main method        def main(args: Array[String])        {            var ta= new classA            ta.ad            ta.ab //error        }    }}Output:error: variable ab in class classA cannot be accessed in testA.classAAccess to protected method ab not permitted becauseenclosing object GfG in package testB is not a subclass ofclass classA in package testA where target is definedta.ab //error^one error found
// Scala program of Protected Scope// package testApackage testA{    class classA     {        protected var ab: Int=4        var ad: Int =1    }}  // another package testBpackage testB{    // importing all the members     // from testA package    import testA._          // Creating object    object GfG    {        // Main method        def main(args: Array[String])        {            var ta= new classA            ta.ad            ta.ab //error        }    }}
Output:
error: variable ab in class classA cannot be accessed in testA.classAAccess to protected method ab not permitted becauseenclosing object GfG in package testB is not a subclass ofclass classA in package testA where target is definedta.ab //error^one error found
Object private is same as private the only difference is that the member declared object private will available only from in which the member is defined, i.e. no object can access it hence therefore named object private.
Object protected is same as protected the only difference is that the member will be only available in which it is defined or to the sub classes and not available to the objects.
To mark an member object private use the keywords private[this].
To mark an member object protected use the keywords protected[this], where this refers or points to the current object.Example :// Scala program of Object Private/Protected Scope// package test1.test11class class11 {    private[this] var x = 1    private var t = 2    var z = 3    def method11(other: class11): Unit =    {        println(x)        println(t)        println(z)                  // println(other.x)        println(other.t)        println(other.z)    }}// here on line14 x can only be// accessed from inside in which // it is defined  // Creating objectobject GfG{    // Main method    def main(arg: Array[String])    {        var obj11 = new class11() //current instance created        var y = 2        println(obj11.method11(obj11))        println(obj11.z)        //println(obj11.t) //error: t cannot be accessed        //println(obj11.x) //error: x is not a member of class11        //according to obj11 x is not a member    }}Output :1
2
3
2
3
()
3

Example :
// Scala program of Object Private/Protected Scope// package test1.test11class class11 {    private[this] var x = 1    private var t = 2    var z = 3    def method11(other: class11): Unit =    {        println(x)        println(t)        println(z)                  // println(other.x)        println(other.t)        println(other.z)    }}// here on line14 x can only be// accessed from inside in which // it is defined  // Creating objectobject GfG{    // Main method    def main(arg: Array[String])    {        var obj11 = new class11() //current instance created        var y = 2        println(obj11.method11(obj11))        println(obj11.z)        //println(obj11.t) //error: t cannot be accessed        //println(obj11.x) //error: x is not a member of class11        //according to obj11 x is not a member    }}
Output :
1
2
3
2
3
()
3

When we want a member to be available to a whole package. It comes to declare that member as private[package_name].
All the member inside the package can access that member.
Member can be accessed by any other package whose name is being qualified to.Example :// Scala program of Package Specific// Scala program of Package Specificpackage aaclass geek{    class g1    {          // inner class        // private to class g1        private var a = 0                   // available to package aa        private[aa] var b = 0         def method()        {            a = a + 1            b = b + 1            println("welcome to inner class g1")            println("a= "+a)        }    }}   // Creating objectobject Main{    // Driver code    def main(args: Array[String])    {        val obj = new geek()        val o = new obj.g1        o.method();        println("b= "+o.b);    }}
// Scala program of Package Specific// Scala program of Package Specificpackage aaclass geek{    class g1    {          // inner class        // private to class g1        private var a = 0                   // available to package aa        private[aa] var b = 0         def method()        {            a = a + 1            b = b + 1            println("welcome to inner class g1")            println("a= "+a)        }    }}   // Creating objectobject Main{    // Driver code    def main(args: Array[String])    {        val obj = new geek()        val o = new obj.g1        o.method();        println("b= "+o.b);    }}
Output :
welcome to inner class g1
a= 1
b= 1

Akanksha_Rai
Picked
Scala
Scala-Method
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Scala List filter() method with example
Scala Tutorial – Learn Scala with Step By Step Guide
Type Casting in Scala
Scala Map
How to get the first element of List in Scala
Class and Object in Scala
Scala Lists
Scala List contains() method with example
Operators in Scala
Inheritance in Scala