- Concepts of Programming Languages

Scala Classes

Instructor:

Learning Objectives

Complex data in object-oriented programming?

  • Identify classes and objects in Scala
  • Use class parameters to initialize, query, and modify object values
  • Use Scala objects to implement singletons
  • Describe methods

Classes

  • Define a class C
    1. class C (f1:Int, val f2:Int, var f3:Int):
    2. //...
  • Has one constructor with parameters f1, f2, f3
  • Instantiate the class C
    1. val c = new C (2, 3, 5)
  • Instance of C is heap allocated
  • c is a reference to instance

Class Parameter Details

  • Class parameters simultaneously declare constructors and accessor methods
  • Components of the class body are public by default

Scala

  1. class C1 (x:Int):
  2. def double() = x+x
  3. end C1
  • x private immutable
  • public constructor
  • public double

Expressed in Java

  1. public class C1 {
  2. private final int x;
  3. public C1(int x) { this.x = x; }
  4. public int double() { return x+x; }
  5. }

Class Parameter Details

  • Immutable class parameters can be initialized but not modified

Scala

  1. class C2 (val x:Int):
  2. def double() = x+x
  3. end C2
  • x public immutable

Expressed in Java

  1. public class C2 {
  2. private final int x;
  3. public C2(int x) { this.x = x; }
  4. public int x() { return x; }
  5. public int double() { return x+x; }
  6. }

Class Parameter Details

  • Mutable class parameters can be changed even after initialization

Scala

  1. class C3 (var x:Int):
  2. def double() = x+x
  3. end C3
  • x public mutable

Expressed in Java

  1. public class C3 {
  2. private int x;
  3. public C3(int x) { this.x = x; }
  4. public int x() { return x; }
  5. public void setX(int x) { this.x = x; }
  6. public int double() { return x+x; }
  7. }

Class Body

  • Class body contains
    • Field declarations (val or var)
    • Constructor code
    • Method definitions
  1. class C (f1:Int, val f2:Int, var f3:Int):
  2. val f4 = f1 * f2
  3. var f5 = f2 * f3
  4. println ("Constructing instance of C")
  5. def m (x:Int) : Int =
  6. // cannot reassign to f1, f2, f4
  7. f3 = f3 + 1
  8. f5 = f5 + 1
  9. f1 * f3 * x
  10. end m
  11. end C

Methods vs. Fields

  • val field: val x = 1; strict, initialized once
  • def non-parameterized method: def y = 2; non-strict, executed every time
  • lazy val: memoized def, initialized on demand

Scala

  1. class C:
  2. val x = 1
  3. def y = 2
  4. lazy val z = x + y
  5. end C

Expressed in Java

  1. public class C {
  2. public final int x = 1;
  3. public int y() { return 2; }
  4. private Integer z = null;
  5. public int z() {
  6. if (z == null) z = x + y();
  7. return z;
  8. }
  9. }

Objects

  • object declares a single instance, accessible through the object name
  • Language support for the singleton design pattern
  1. object C:
  2. var count = 0
  3. end C
  4. C.count = C.count + 1

Objects

  • Singleton objects object are instantiated on program startup
  • Method main must be declared in an object

Java

  1. public class C {
  2. public static void main (String[] args) {
  3. //...
  4. }
  5. }

Scala

  1. object C:
  2. def main (args:Array[String]) : Unit =
  3. //...
  4. end main
  5. end C

Companion Objects

Java: static vs. non-static

  • static components belong to a class
  • All other belong to instance of class
  1. class C {
  2. private int f1 = 0;
  3. private static int f2 = 0;
  4. public int m1 () {
  5. C.f2 = 3;
  6. return f1;
  7. }
  8. public static int m2 (C other) {
  9. other.f1 = 5;
  10. return f2;
  11. }
  12. }

Scala: Companion object replaces static

  • All data and methods belong to objects
  • Companion related to other instances
  1. class C:
  2. private var f1 = 0
  3. def m1 () =
  4. C.f2 = 3
  5. f1
  6. end C
  7. object C:
  8. private var f2 = 0
  9. def m2 (other: C) =
  10. other.f1 = 5
  11. f2
  12. end C

Coding Exercise

  • Matrix class

Summary

  • Class parameters and factory methods replace constructors
  • Builtin support for the Singleton design pattern

Classes

  1. class C
  2. ( f: Int,
  3. val g: Int,
  4. var h: Int)
  • f private immutable
  • g public immutable
  • h public mutable

Objects

  • Replace static
  1. object O:
  2. val n = 10
  3. end O
  4. val n = O.n

Companion Objects

  1. class X:
  2. private val n = 10
  3. object X:
  4. def getN(x: X) = x.n
  5. val n = X.getN(new X)
  • Have access to private elements of companions

# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Immutable vs. Mutable Fields <div class="grid grid-cols-2 gap-4"> <div> ## Scala ```scala class C: val x = 1 var z = 1 end C ``` </div> <div> ## Expressed in Java ```java public class C { private final int x = 1; private int z = 1; public int x() { return x; } public int z() { return z; } public void z_$eq(int z) { this.z = z; } } ``` </div> </div> ---

- Many languages distinguish data and methods belonging to _an instance_ vs. _a class_