Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, the visibility of constructor fields is controlled through access modifiers and annotations. Here's how you can control the visibility of fields in a class's primary constructor:
class Person(name: String, age: Int) val person = new Person("Alice", 30) // person.name or person.age won't compile
val
and var
:
By prefixing a constructor parameter with val
or var
, it becomes a public field. A val
makes it a read-only (immutable) field, and var
makes it a mutable field.class Person(val name: String, var age: Int) val person = new Person("Alice", 30) println(person.name) // Accessible and immutable person.age = 31 // Accessible and mutable
private
:
By prefixing a constructor parameter with private
, it becomes a private field, inaccessible from outside the class.class Person(private val name: String, private var age: Int) val person = new Person("Alice", 30) // person.name and person.age are not accessible from outside the class
protected
:
By prefixing a constructor parameter with protected
, it becomes a field that is accessible from subclasses and within the same package, but not from other locations.class Person(protected val name: String, protected var age: Int)
private[this]
:
The private[this]
modifier makes the field accessible only within the current instance of the class. It's more restrictive than the regular private
modifier.class Person(private[this] val name: String, private[this] var age: Int) { def isSameName(other: Person): Boolean = { // other.name won't compile because of private[this] this.name == this.name } }
val
fields by default.case class Person(name: String, age: Int) val person = new Person("Alice", 30) println(person.name) // Accessible and immutable
In summary, by using these modifiers and annotations, you can control the visibility of constructor fields in Scala, thus adhering to encapsulation principles in object-oriented design.
Access Modifiers for Constructor Fields in Scala:
Access modifiers control the visibility of constructor fields. private
restricts access to the class, and protected
allows access to subclasses.
class Person(private val name: String, protected var age: Int)
Private vs. Protected Constructor Fields in Scala:
private
restricts access to the declaring class, while protected
allows access to the declaring class and its subclasses.
class Student(private val studentId: String, protected var grade: Char)
Controlling Visibility of Fields in Primary Constructors:
Visibility modifiers can be applied directly to primary constructor parameters.
class Car(private val model: String, protected var price: Double)
Secondary Constructors and Field Visibility in Scala:
Secondary constructors can use different visibility modifiers for fields.
class Book(private val title: String, protected var author: String) { def this(title: String) { this(title, "Unknown") // Secondary constructor } }
Restricting Access to Mutable Constructor Fields:
To restrict modification of constructor fields, use val
for immutability.
class Temperature(private val celsius: Double) { def toFahrenheit: Double = celsius * 9 / 5 + 32 }
Visibility Modifiers for Case Class Constructor Fields:
Case class constructor fields have implicit visibility modifiers based on their use.
case class Point(private val x: Int, protected var y: Int)
Companion Objects and Constructor Field Visibility in Scala:
Companion objects can access private constructor fields.
object Point { def printX(point: Point): Unit = { println(point.x) // Accessing private constructor field } }
Immutability and Constructor Field Visibility in Scala:
Immutability is encouraged for constructor fields to ensure a more predictable and functional code style.
class Address(val street: String, val city: String, val zipCode: String)
Here, val
indicates immutability for each field.