Scala is an Object Oriented Programming language just like Java. That's why, class is the building block of Scala as well.
Class
Class is defined the same way as defined in Java. Keyword class is used to define a class and this keyword is used by instance to refer itself.
Instantiating Class (Constructors)
Scala provides the default no-argument constructor just like Java does for each class. Custom constructors can be defined with class name, which is a simplified form of defining constructors in Java.
class Point(xc: Int, yc: Int) {
var x: Int = xc;
var y: Int = yc;
Above example shows that co-ordinates for Point class can be passed to its primary constructor and can be assigned to instance variables directly.
But does that mean we can define only one constructor for a class? No, Scala supports multiple auxiliary constructor but single primary constructor. This essentially means that the constructor defined along with definition of the class is treated as primary constructor while any other constructor defined in class body will be auxiliary constructor. Auxiliary constructor is dependent on primary constructor to define the instance variables. It has to make a call to primary constructor in order to instantiate the class.
class Point(xc: Int, yc: Int) {
var x: Int = xc;
var y: Int = yc;
def this(x: Int){
this(x, 0)
}
Above code shows a auxiliary constructor which creates a point on Y -axis by passing a value for X-aixs and 0 for Y-axis.
As auxiliary constructor can only call primary constructor, so what if class is derived from another class. How auxiliary constructor will call the super constructor? It cannot do that directly, which means it can only call primary constructor but primary constructor can make call to super constructor. That's how auxiliary constructor can make a call to super constructor through primary constructor.
So in Scala there can be only one primary constructor, rest all are dependent on that unlike Java where each constructor is a separate entity.
Validating Constructor Arguments
As primary constructor is not defined as method in Scala, how to validate the arguments passed? Here it is:
class Point(xc: Int, yc: Int) {
var x: Int = xc;
var y: Int = yc;
require((x == -1 || y == -1), "Negative Co-ordinates are not allowed")
As shown above, method require can be used to validate the arguments passed to constructor. in above example if one of the co-ordinate is negative, an Exception will be thrown with message "Negatives Co-ordinates are not allowed".
Visibility Rules
Most of the visibility rules in Scala are same as Java only with some differences like:
- If inner class has a private field even outer class cannot see that variable
- Default visibility in Scala is Public while its Default in Java.
Rest all visibility scopes like private, public, protected are applicable in Scala with same meaning as in Java.
Setters and Getters
Getter can be defined explicity as a method like:
def xVal = x which is equivalent to
def xVal = {
return x;
}
So everything like curly braces, return statement is optional. Scala identifies that method is returning something by '=' sign and the return type is interpreted by the type of returned variable.