Open In App

Controlling Method Scope In Scala

Last Updated : 06 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report


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:

  1. Public Scope
  2. Private Scope
  3. Protected Scope
  4. Object-private Scope
  5. Package Specific
Public Scope
  • 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 testA
    class classA
    {
        def method1(): Unit=
        {
            println("method1")
        }
    }
      
    // Creating object
    object 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 Scope
  • 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 testA
    class classA 
    {
        var x = 1
        private def method1: Unit =
        {
            println("method1")
        }
    }
      
    // Creating object
    object 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
    
Protected Scope
  • 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 test
    class classab
    {
        protected var ab: Int=4
        var ad: Int =1
    }
      
    // Creating object
    object 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 testA
    package testA
    {
        class classA 
        {
            protected var ab: Int=4
            var ad: Int =1
        }
    }
      
    // another package testB
    package 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.classA
    Access to protected method ab not permitted because
    enclosing object GfG in package testB is not a subclass of
    class classA in package testA where target is defined
    ta.ab //error
    ^
    one error found

Object Private/Protected Scope
  • 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.test11
    class 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 object
    object 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
    
Package Specific
  • 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 Specific
    package aa
    class 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 object
    object 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads