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, classes come with built-in support for getters and setters, making it easy to encapsulate fields. Unlike Java, where you'd typically write explicit getter and setter methods, Scala provides a more concise way of handling this.
When you declare a class variable as var
, Scala automatically generates getters and setters for it:
class Person { var name: String = _ }
For the above code:
def name: String
def name_=(value: String): Unit
You can use them as:
val person = new Person person.name = "Alice" // calls the setter println(person.name) // calls the getter
If you need custom behavior in your getters and setters, you can define them explicitly:
class Person { private var _age: Int = 0 // Getter def age: Int = _age // Setter def age_=(value: Int): Unit = { if (value > _age) { _age = value } } }
In the above code:
_age
.age
just returns the _age
.age_=
only updates the age if the new value is greater than the current age.val
If you declare a class property using val
, it becomes read-only, and only a getter is generated:
class Person { val birthYear: Int = 1990 }
In this case, you can read birthYear
, but you can't change it after the object is constructed.
If you want to make the getter and/or setter private, you can use access modifiers:
class Person { private var _name: String = _ def name: String = _name // Public getter private def name_=(value: String): Unit = { // Private setter _name = value } }
In this example, the setter for name
is private, so it cannot be accessed outside of the Person
class.
Scala's approach to getters and setters is concise and expressive. It eliminates boilerplate while still offering the flexibility to customize the behavior when needed. This is another example of how Scala blends object-oriented and functional programming paradigms to offer a powerful programming model.
Scala class properties and accessors:
class Person(var name: String, var age: Int) val person = new Person("Alice", 25) println(person.name) // Accessing the property
Defining getters and setters in Scala:
var
properties, allowing access and modification.class Person(var name: String, var age: Int) val person = new Person("Bob", 30) person.age = 35 // Using the setter
Custom getter and setter methods in Scala:
class Person(private var _age: Int) { def age: Int = _age def age_=(newAge: Int): Unit = { if (newAge >= 0) _age = newAge } } val person = new Person(25) person.age = 30 // Using the custom setter
Immutable properties in Scala:
val
instead of var
creates immutable properties that cannot be modified after initialization.class Circle(val radius: Double) val circle = new Circle(5.0) // circle.radius = 7.0 // Error: Cannot reassign to val
Using case classes for automatic getters and setters:
case class Point(x: Double, y: Double) val origin = Point(0.0, 0.0) println(origin.x) // Automatic getter
Scala val vs. var and property mutability:
val
declares immutable properties, while var
declares mutable properties.class Student(val name: String, var age: Int) val student = new Student("Charlie", 21) // student.name = "David" // Error: Cannot reassign to val
Getter and setter visibility in Scala:
class Person { private var _age: Int = 0 def age: Int = _age private def age_=(newAge: Int): Unit = { if (newAge >= 0) _age = newAge } }
Access control for getters and setters in Scala:
private
, protected
, and public
can be used to control access to properties.class BankAccount(private var balance: Double) { def deposit(amount: Double): Unit = { if (amount > 0) balance += amount } def getBalance: Double = balance }
Lazy initialization with getters in Scala:
class ExpensiveResource { // Assume expensive initialization } class MyClass { lazy val resource: ExpensiveResource = new ExpensiveResource }
Property naming conventions in Scala:
class Employee(private val employeeId: String, var salary: Double)
Overriding getters and setters in Scala:
class CelsiusTemperature(var celsius: Double) { def fahrenheit: Double = celsius * 9 / 5 + 32 def fahrenheit_=(f: Double): Unit = { celsius = (f - 32) * 5 / 9 } }
Property observers and getters in Scala:
didSet
and willSet
in Swift) are not directly available in Scala. However, custom logic can be added to getters and setters.class Temperature(private var _celsius: Double) { def celsius: Double = _celsius def celsius_=(newCelsius: Double): Unit = { // Custom logic _celsius = newCelsius } }
Scala self-types and property access:
trait Logger { def log(message: String): Unit } trait UserService { this: Logger => def performAction(): Unit = { log("Action performed") } }