The restriction of scope to certain places is realized with the help of access modifiers. public, private and protected are the three access modifiers used in Scala. Include the keywords private/protected/public in the definition of class, object or package to enforce the corresponding scope. If none of these keywords are used, then the access modifier is public by default.

Private member

Private members are available only inside the class or to the object containing the member definition. The private access modifier can be used for variables and methods as well. Consider an example to show the private members;

Here we define an outer class Student and an inner class Student1 with variable “sage” which is a private member.

In the StudentAge class we just access and add 4 to the sage(student age) variable. Now when we try accessing the variable sage from another object instantiated as “new Student1”, an access error is thrown as below.

In Student1 class we also have defined a private method studage() in which we are just printing the student age. We are then calling this method from within StudentAge class. This is allowed as the Student age class is the innermost nested class of Student1. studage method cannot be accesed from StudentAge class.

Run the above example code to see the error message as shown below;

Below image shows the complete code execution in Scala shell.


Protected Member

Protected members can be accessed from subclasses of the class in which the member is defined (inherited class). This can also be used for methods as well as variables.

Consider an example shown below;

The class Student is created consisting of a variable sid and a method studId() in which we are printing the student id.

Next, we are creating a class CollegeStudent extending the super class Student and incrementing the variable sid by 6 and then invoke the method studId().

We create another class TestStudent and try to the access the sid variable and then call studid() after creating a new object of the Student class. Since TestStudent is not a subclass of Student, an error is thrown as sid and studId() are not accessible. This is because they are specified as protected members and can only be accessed by subclassing the main class. Class CollegeStudent is a subclass of Student and hence can access sid variable and studId method.

Run the above code to see the following error in output.

But when we try to run the program and access the variable and method from inside CollegeStudent class, there won’t be any errors.


Public member

The default access modifier is public. When the keywords private or protected is not specified then the members are treated as public by the compiler and we don’t have to explicitly specify the keyword “public”.

Consider an example to depict the public members;

Student class is created with an inner class Stud along with variables sname and method studName which prints the student name.

TestStud class is created calling the studName() method. Now when we try accessing the sname variable or invoke studName() outside the Stud class, it becomes accessible since attributes with access modifier public is visible to any of the classes.

When we execute the above code the Student class is created successfully without any errors.

The following table depicts the accessibility of various access modifiers for class, package, subclass and all classes.

Modifier Class Package Subclass Every class
public Yes Yes Yes Yes
protected Yes No Yes No
private Yes No No No

Scope of Protection

Access modifiers can be suffixed with qualifiers in the []. The modifiers of type protected[Y] or private[Y] specifies that access is provided upto Y where Y can be a package, class or singleton object.

Consider an example to depict the usage of these qualifiers

To type the above code in the shell we have to first enter the paste mode by typing :paste -raw and then insert this code. Once the code is inserted completely press ctrl-D to exit from the paste mode and run the code (enter key). In the normal mode an error is thrown as illegal start of definition after declaring the package.

Here we are declaring packages student and graduatestudent. We are defining a class Stud with the variable “degree” having protected access modifier qualified by the package name graduatestudent, variable marks of private access qualified to student package and age variable for the current instance of the Stud class. studdegree() method is defined in which we are trying to access the variables degree, marks and age. The variable age cannot be accessed and so it throws an error as below.

Key points about the variables degree, marks and age

  • The variable degree can be accessed by any class within the package graduatestudent
  • The variable marks can be accesed by any class within the package student.
  • The variable age can be accessed only on the implicit objects within instance methods(this).

That’s all for access modifiers in Scala, its very close to java access modifiers except the default modifier is public here. We will look more Scala core features in the coming posts.

By admin

Leave a Reply

%d bloggers like this: