Scala Classes and Objects
Classes are abstractions of objects, and objects are specific instances of classes. Classes are abstract and do not occupy memory, while objects are concrete and take up storage space. A class is a blueprint for creating objects; it is a software template that defines the methods and variables included in objects of a specific type.
We can create objects of a class using the new
keyword, as shown in the following example:
Example
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println("x coordinate: " + x)
println("y coordinate: " + y)
}
}
In Scala, classes are not declared as public, and a Scala source file can contain multiple classes.
The class defined in the example above has two variables x and y, and one method: move, which has no return value.
Scala class definitions can have parameters, known as class parameters, such as xc, yc in the example above, and class parameters can be accessed throughout the class.
We can then use new
to instantiate the class and access its methods and variables:
Example
import java.io._
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println("x coordinate: " + x)
println("y coordinate: " + y)
}
}
object Test {
def main(args: Array[String]) {
val pt = new Point(10, 20)
// Move to a new position
pt.move(10, 10)
}
}
Executing the above code will produce the following output:
$ scalac Test.scala
$ scala Test
x coordinate: 20
y coordinate: 30
Scala Inheritance
In Scala, inheriting a base class is similar to Java, but we need to pay attention to the following points:
- Overriding a non-abstract method must use the
override
modifier.
- Overriding a non-abstract method must use the
- Only the primary constructor can pass parameters to the base class constructor.
- When overriding an abstract method of the superclass in a subclass, you do not need to use the
override
keyword.
- When overriding an abstract method of the superclass in a subclass, you do not need to use the
Let's look at an example:
Example
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println("x coordinate: " + x)
println("y coordinate: " + y)
}
}
class Location(override val xc: Int, override val yc: Int,
val zc: Int) extends Point(xc, yc) {
var z: Int = zc
def move(dx: Int, dy: Int, dz: Int) {
x = x + dx
y = y + dy
z = z + dz
println("x coordinate: " + x)
println("y coordinate: " + y)
println("z coordinate: " + z)
}
}
Scala uses the extends
keyword to inherit a class. In the example, the Location
class inherits from the Point
class. Point
is referred to as the parent class (base class), and Location
is referred to as the subclass.
override val xc
is an override of the parent class's field.
Inheritance inherits all the properties and methods of the parent class, and Scala only allows inheritance from one parent class.
Here is an example:
Example
```scala import java.io._
class Point(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println("x coordinate: " + x) println("y coordinate: " + y) } }
class Location(override val xc: Int, override val yc: Int, val zc: Int) extends Point(xc, yc) { var z: Int = zc
def move(dx: Int, dy: Int, dz: Int) { x = x + dx y = y + dy z = z + dz println("x coordinate