In the latest installment of his IBM developerWorks series, Packages and access modifiers, Ted Neward explains how Scala's package and access modifiers offer potentially greater flexibility than similar constructs do in Java:
Scala's packaging, import, and access modifier mechanisms provide the fine degree of control and encapsulation that a traditional Java programmer has never enjoyed. For example, they offer the ability to import select methods of an object, making them appear as global methods, without the traditional drawbacks of global methods; they make working with those methods exceedingly easy, particularly if those methods are methods that provide higher-order functionality...
Scala takes a slightly different approach in respect to packaging, treating it as a combination of the Java language's declaration approach and C#'s scoped approach. With that in mind, a Java developer can do the traditional Java approach and put a package declaration at the top of a .scala file just as normal Java classes do; the package declaration applies across the entire file scope just as it does in Java code. Alternatively, a Scala developer can use Scala's package "scoping" approach in which curly braces delimit the scope of the package statement...
As well, Neward shows that in Scala, the import statement is not relegated to the beginning of a file, but can be used anywhere in code, and that using import that way provides a scope for the imported classes.
Neward also discusses access modifiers in Scala, pointing out that:
Scala:
Does away with package-level qualification (in a way)
Uses "public" by default
Specifies "private" to mean "accessible only to this scope"
"protected" is definitely different from its counterpart in Java code; where a Java protected member is accessible to both subclasses and the package in which the member is defined, Scala chooses to grant access only to subclasses. This means that Scala's version of protected is more restrictive (although arguably more intuitively so) than the Java version.
Finally, Neward's article discusses more fine-grained uses of access modifiers, such as the ability to qualify an access modifier with a package name, for instance.
What do you think of Scala's packages and access modifiers? Do you think the added flexibility would solve problems you're currently encountering in your Java code?