Controlling Method Scope In Scala
Last Updated :
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 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 :
class classA
{
def method 1 () : Unit =
{
println( "method1" )
}
}
object GfG
{
def main(args : Array[String])
{
var x = new classA
x.method 1 ()
}
}
|
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 :
class classA
{
var x = 1
private def method 1 : Unit =
{
println( "method1" )
}
}
object GfG
{
def main(arg : Array[String])
{
var obj 1 = new classA
printf( "x = " +obj 1 .x)
}
}
|
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 :
class classab
{
protected var ab : Int = 4
var ad : Int = 1
}
object GfG extends classab
{
def main(args : Array[String])
{
println(ab)
println(ad)
}
}
|
Output:
4
1
- Protected members cannot be accessed by other members in other packages even with imports.
Example :
package testA
{
class classA
{
protected var ab : Int = 4
var ad : Int = 1
}
}
package testB
{
import testA. _
object GfG
{
def main(args : Array[String])
{
var ta = new classA
ta.ad
ta.ab
}
}
}
|
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 :
class class 11
{
private [ this ] var x = 1
private var t = 2
var z = 3
def method 11 (other : class 11 ) : Unit =
{
println(x)
println(t)
println(z)
println(other.t)
println(other.z)
}
}
object GfG
{
def main(arg : Array[String])
{
var obj 11 = new class 11 ()
var y = 2
println(obj 11 .method 11 (obj 11 ))
println(obj 11 .z)
}
}
|
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 :
package aa
class geek
{
class g 1
{
private var a = 0
private [aa] var b = 0
def method()
{
a = a + 1
b = b + 1
println( "welcome to inner class g1" )
println( "a= " +a)
}
}
}
object Main
{
def main(args : Array[String])
{
val obj = new geek()
val o = new obj.g 1
o.method();
println( "b= " +o.b);
}
}
|
Output :
welcome to inner class g1
a= 1
b= 1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...