Overriding Accessors and Mutators in Scala
Last Updated :
30 Sep, 2022
A standardized way to override default Accessors and Mutators hasn't been fixed by Scala, yet users of this programming language use one specific and a common method for overriding which is discussed in detail below.
Before we get started, what are Accessors and Mutators?
Accessors and Mutators simply can be said as get and set methods respectively. Accessors are used for accessing the data by using variables or constants, helping a user to retrieve the information, working similar to a 'Get' method in Java and Mutators, meaning to change, wherein the variables are changed by a call to function and assigned a new value, they work analogues to 'Set' method in Java.
Consider the following Java code using set and get methods for a class Employee:
Java
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
If you try to write a code for a class named Employee with a constructor parameter named 'name' and attempt to create get and set methods, according to the Scala conventions, your code won’t compile.
// error: this won't work
class Employee(private var name: String) {
def name = name
def name_=(aName: String) { name = aName }
}
On compilation of this code, following errors are produced:
Person.scala:3: error: overloaded method name needs result type
def name = name
^
Person.scala:4: error: ambiguous reference to overloaded definition,
both method name_= in class Employee of type (aName: String)Unit
and method name_= in class Employee of type (x$1: String)Unit
match argument types (String)
def name_=(aName: String) { name = aName }
^
Person.scala:4: error: method name_= is defined twice
def name_=(aName: String) { name = aName }
^
three errors found
The Scala compiler complains about the collision of the names between the 'name' field and the 'name' method. A simple way to solve this error is to add an underscore to the field variable. This resolves the ambiguity.
Note: The underscore is used in prefix position rather than suffix position to avoid any danger of mistakenly typing 'name _' instead of 'name_'. A mistake in typing the other way round could potentially lead to a very confusing error since the heavy use of Scala’s type inference.
class Employee(private var _name: String) {
def name = _name // accessor
def name_=(aName: String) { _name = aName } // mutator
}
How to call the constructor methods are as follows:
val e = new Employee("Adelicia")
e.name = "Alice" // setter
println(e.name) // getter
There are other approaches too for naming getter and setter methods.
Similar Reads
Method Overriding in Scala Method Overriding in Scala is identical to the method overriding in Java but in Scala, the overriding features are further elaborated as here, both methods as well as var or val can be overridden. If a subclass has the method name identical to the method name defined in the parent class then it is k
8 min read
Method Overriding in Scala Method Overriding in Scala is identical to the method overriding in Java but in Scala, the overriding features are further elaborated as here, both methods as well as var or val can be overridden. If a subclass has the method name identical to the method name defined in the parent class then it is k
8 min read
Method Overloading in Scala Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. Scala can distinguish the methods with different method sig
5 min read
Access Modifiers in Scala Access Modifiers in scala are used to define the access field of members of packages, classes or objects in scala.For using an access modifier, you must include its keyword in the definition of members of package, class or object.These modifiers will restrict accesses to the members to specific regi
3 min read
Scala | Field Overriding In any object-oriented programming language, Overriding is a feature that allows a subclass to provide a specific implementation of a method or field that is already provided by one of its super-classes. In Scala, Overriding is further explicitly stated in comparison to Overriding in Java, as here b
4 min read
Interesting fact about Scala Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source
3 min read