Controlling Method Scope In Scala Last Updated : 06 Nov, 2019 Comments Improve Suggest changes Like Article Like 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: Public Scope Private Scope Protected Scope Object-private Scope 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 // 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 // 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 // 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 // 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 // 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 // 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 Comment More infoAdvertise with us Next Article Controlling Method Scope In Scala P Patabhu Follow Improve Article Tags : Scala Scala Scala-Method Similar Reads Methods to call on a Set in Scala A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala. 13 min read Scala Set init() method with example The init() method is utilized to find all the elements of the set except the last one. Method Definition: def init: Set[A] Return Type: It returns all the elements of the set except the last one. Example #1: Scala // Scala program of init() // method // Creating object object GfG { // Main method de 1 min read Scala Set find() method with example The find() method is utilized to find the first element of the set that satisfies the given predicate if present. Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element of the set which satisfies the given predicate. Example #1: Scala // Scala program 1 min read Scala | Methods to Call Option The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null. There are a few methods that 5 min read Scala map contains() method with example The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the Map interface of Scala. It checks whether the stated map contains a binding for a key or 2 min read Like