Fundamentals 5 min read

Scala Access Modifiers: private, protected, and public

This article explains Scala's access modifiers—private, protected, and public—including their default visibility, stricter rules compared to Java, scoped protection syntax, and multiple code examples illustrating how these modifiers affect member accessibility in nested and package structures.

Big Data Technology & Architecture
Big Data Technology & Architecture
Big Data Technology & Architecture
Scala Access Modifiers: private, protected, and public

Scala's access modifiers are similar to Java's, consisting of private, protected, and public. If no modifier is specified, members are public by default.

Private Members

Members marked with private are visible only inside the class or object that defines them, and the rule also applies to inner classes.

class Outer{
    class Inner{
    private def f(){println("f")}
    class InnerMost{
        f() // correct
        }
    }
    (new Inner).f() // illegal
}

In the example, (new Inner).f() is illegal because f is declared private inside Inner, so it cannot be accessed from outside that class. However, InnerMost can call f because it is nested within Inner. Java allows outer classes to access private members of inner classes, but Scala does not.

Protected Members

Scala's protected access is stricter than Java's: a protected member can be accessed only from subclasses of the class that defines it, not from other classes in the same package.

package p{
class Super{
    protected def f() {println("f")}
    }
    class Sub extends Super{
        f()
    }
    class Other{
        (new Super).f() // illegal
    }
}

Here, Sub can call f because it inherits from Super. Other cannot call f because it does not extend Super, even though it resides in the same package—a behavior that Java would allow.

Public Members

If no modifier is specified, Scala treats the member as public, meaning it can be accessed from anywhere.

class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // correct
      }
   }
   (new Inner).f() // correct because f() is public
}

Scope Protection

Scala allows more granular visibility using qualified modifiers:

private[x]

or

protected[x]

Here, x can be a package, class, or singleton object. For example, private[bobsrockets] makes a member visible to all classes and objects inside the bobsrockets package but private to everything else.

package bobsrocckets{
    package navigation{
        private[bobsrockets] class Navigator{
         protected[navigation] def useStarChart(){}
         class LegOfJourney{
             private[Navigator] val distance = 100
             }
            private[this] var speed = 200
            }
        }
        package launch{
        import navigation._
        object Vehicle{
        private[launch] val guide = new Navigator
        }
    }
}

In this example, Navigator is visible to all code within the bobsrockets package, useStarChart is visible only to subclasses within the navigation package, and distance is visible only to code inside Navigator. Such scoped visibility is useful for large projects that span multiple packages.

programming fundamentalsScalaPublicProtectedAccess ModifiersPrivate
Big Data Technology & Architecture
Written by

Big Data Technology & Architecture

Wang Zhiwu, a big data expert, dedicated to sharing big data technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.