- Scala - Files I/O
- Scala - Extractors
- Scala - Exception Handling
- Scala - Regular Expressions
- Scala - Pattern Matching
- Scala - Traits
- Scala - Collections
- Scala - Arrays
- Scala - Strings
- Scala - Closures
- Scala - Functions
- Scala - Loop Statements
- Scala - IF ELSE
- Scala - Operators
- Scala - Access Modifiers
- Scala - Classes & Objects
- Scala - Variables
- Scala - Data Types
- Scala - Basic Syntax
- Scala - Environment Setup
- Scala - Overview
- Scala - Home
Scala Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Scala - Access Modifiers
This chapter takes you through the Scala access modifiers. Members of packages, classes or objects can be labeled with the access modifiers private and protected, and if we are not using either of these two keywords, then access will be assumed as pubpc. These modifiers restrict accesses to the members to certain regions of code. To use an access modifier, you include its keyword in the definition of members of package, class or object as we will see in the following section.
Private Members
A private member is visible only inside the class or object that contains the member definition.
Following is the example code snippet to explain Private member −
Example
class Outer { class Inner { private def f() { println("f") } class InnerMost { f() // OK } } (new Inner).f() // Error: f is not accessible }
In Scala, the access (new Inner). f() is illegal because f is declared private in Inner and the access is not from within class Inner. By contrast, the first access to f in class Innermost is OK, because that access is contained in the body of class Inner. Java would permit both accesses because it lets an outer class access private members of its inner classes.
Protected Members
A protected member is only accessible from subclasses of the class in which the member is defined.
Following is the example code snippet to explain protected member −
Example
package p { class Super { protected def f() { println("f") } } class Sub extends Super { f() } class Other { (new Super).f() // Error: f is not accessible } }
The access to f in class Sub is OK because f is declared protected in ‘Super’ class and ‘Sub’ class is a subclass of Super. By contrast the access to f in ‘Other’ class is not permitted, because class ‘Other’ does not inherit from class ‘Super’. In Java, the latter access would be still permitted because ‘Other’ class is in the same package as ‘Sub’ class.
Pubpc Members
Unpke private and protected members, it is not required to specify Pubpc keyword for Pubpc members. There is no exppcit modifier for pubpc members. Such members can be accessed from anywhere.
Following is the example code snippet to explain pubpc member −
Example
class Outer { class Inner { def f() { println("f") } class InnerMost { f() // OK } } (new Inner).f() // OK because now f() is pubpc }
Scope of Protection
Access modifiers in Scala can be augmented with quapfiers. A modifier of the form private[X] or protected[X] means that access is private or protected "up to" X, where X designates some enclosing package, class or singleton object.
Consider the following example −
Example
package society { package professional { class Executive { private[professional] var workDetails = null private[society] var friends = null private[this] var secrets = null def help(another : Executive) { println(another.workDetails) println(another.secrets) //ERROR } } } }
Note − the following points from the above example −
Variable workDetails will be accessible to any class within the enclosing package professional.
Variable friends will be accessible to any class within the enclosing package society.
Variable secrets will be accessible only on the imppcit object within instance methods (this).