Immutable and mutable objects, references and the heapProgramming Practice (CS109)Number representationsClasses and objects I

Classes and objects I

Objects are the basis of object-oriented programming. In Scala, every piece of data is an object. Each object has a type, such as Int, Double, String, tuple, or Array[Int]. The type of an object determines what you can do with the object. When you use an object, you should think of the object as a black box, and you need not know what really happens inside the object. All you need to know is what the object does, not how it implements its functionality.

A class defines a new type of object. You can also think about a class as a "blueprint" for objects. Once you define a class, you can create objects from the blueprint using the keyword new.

Case classes

A common use of classes is to define objects with a number of attributes. For instance:

In Scala, such a simple class is best implemented as a case class:

scala> case class Point(x: Int, y: Int)
defined class Point
Point represents a two-dimensional point. It has two fields, namely x and y. We can create Point objects as follows:
scala> var p = new Point(2, 5)
p: Point = Point(2,5)

Normally, new objects are created using the keyword new. We have already seen it when we created arrays, for instance. For case classes the Scala compiler allows us to omit the new keyword. (This only works for case classes, but not for more general classes that we will discuss later):

scala> var p = Point(2, 5)
p: Point = Point(2,5)
This looks like a function call, and in fact it is a call to the constructor for the Point class.

Once we have a Point object, we can access its fields using dot-syntax:

scala> p.x
res0: Int = 2
scala> p.y
res1: Int = 5

We can also print Point objects using println (or by using their toString method):

scala> println(p)
Point(2,5)
scala> p.toString
res1: String = Point(2,5)

We can compare two Point objects using == and !=. Two case classes are equal if and only if all their fields are equal.

 
scala> val q = Point(7, 19)
q: Point = Point(7,19)
scala> val r = Point(2, 5)
r: Point = Point(2,5)
scala> p == r
res2: Boolean = true
scala> p == q
res3: Boolean = false
scala> p != q
res4: Boolean = true

Let's now look at the other examples from above: A date object could be defined like this:

scala> case class Date(year: Int, month: Int, day: Int)
defined class Date
scala> val d = Date(2012, 2, 20)
d: Date = Date(2012,2,20)
scala> d.month
res5: Int = 2
scala> d.day
res6: Int = 20
A student object might look like this:
scala> case class Student(name: String, id: Int, dept: String)
defined class Student
scala> val s = Student("Otfried", 13, "CS")
s: Student = Student(Otfried, 13, CS)
scala> s.id
res7: Int = 13
And a Blackjack card object could look like this:
scala> case class Card(face: String, suit: String)
defined class Card
scala> val c = Card("Ace", "Diamonds")
c: Card = Card(Ace,Diamonds)
scala> c.suit
res8: String = Diamonds

Using library classes

In our programs we will use many classes that have been defined by others and made available in a library.

In Scala, it is not necessary to declare classes that one wants to use. For instance, you can use the value \(\pi\) in Scala without importing the math package, by just writing the full name math.Pi of the value:

scala> math.Pi
res0: Double = 3.141592653589793
(In Python we would have to use import, in C or C++ we would have had to use a #include statement.)

The Scala compiler and run time system look for the classes automatically based on the full class name.

Often it is cumbersome to use a long class name, like math.Pi or scala.io.Source. In that case we can use an import statement to import the name of the class or object into our program:

scala> import math.Pi
import math.Pi
scala> Pi
res7: Double = 3.141592653589793

To import all the names from a package, for instance the math package, you can use this:

scala> import math._
import math._
scala> (sin(Pi/2), cos(Pi/3), sqrt(2))
res8: (Double, Double, Double) = (1.0,0.5,1.4142135623730951)

Even though Scala is a new language, there are already many libraries we can use, since Scala can also use Java libraries. There are thousands of classes in various Java libraries, organized into packages. You can find useful packages and reuse those. You can find links to the Scala libraries and Java libraries on the documentation page.

Immutable and mutable objects, references and the heapProgramming Practice (CS109)Number representationsClasses and objects I