1. Class Hierarchies
1.1 Abstract Classes
Consider the task of writing a class for sets of integers with the following operations.
IntSet is an abstract class.
Abstract classes can contain members which are missing an implementation (in our case,
Consequently, no instances of an abstract class can be created with the operator
1.2 Class Extensions
Let’s consider implementing sets as binary trees.
There are two types of possible trees: a tree for the empty set, and a tree consisting of an integer and two sub-trees.
Here are their implementations:
Empty and NonEmpty both extend the class
This implies that the types
NonEmpty conform to the type
- an object of type
NonEmptycan be used wherever an object of type
1.3 Base Classes and Subclasses
IntSet is called the superclass of
NonEmpty are subclasses of
In Scala, any user-defined class extends another class.
If no superclass is given, the standard class
Object in the Java package
java.lang is assumed.
The direct or indirect superclasses of a class C are called base classes of C .
So, the base classes of
1.4 Implementation and Overriding
The definitions of
incl in the classes
NonEmpty implement the abstract functions in the base trait
It is also possible to redefine an existing, non-abstract definition in a subclass by using
1.5 Object Definitions
This defines a singleton object named
Empty instances can be (or need to be) created.
Singleton objects are values, so Empty evaluates to itself.
Run Scala Application Program in command line:
In command line:
Write a method
union for forming the union of two sets. You should implement the following abstract class.
1.6 Dynamic Binding
Object-oriented languages (including Scala) implement dynamic method dispatch.
This means that the code invoked by a method call depends on the runtime type of the object that contains the method.
Another evaluation using
2. How Classes are Organized
Classes and objects are organized in packages.
To place a class or object inside a package, use a
package clause at the top of your source file.
This would place
Hello in the package
You can then refer to
Hello by its fully qualified name
progfun.examples.Hello . For instance, to run the Hello program:
Say we have a class
Rational in package
You can use the class using its fully qualified name:
Alternatively, you can use an
Imports come in several forms:
The first two forms are called named imports.
The last form is called a wildcard import.
You can import from either a package or an object.
Some entities are automatically imported in any Scala program.
- All members of package scala
- All members of package java.lang
- All members of the singleton object scala.Predef
In Java, as well as in Scala, a class can only have one superclass.
But what if a class has several natural supertypes to which it conforms or from which it wants to inherit code?
Here, you could use
A trait is declared like an abstract class, just with trait instead of abstract class
Classes, objects and traits can inherit from at most one class but arbitrary many traits.
Traits resemble interfaces in Java, but are more powerful because they can contains fields and concrete methods.
On the other hand, traits cannot have (value) parameters, only classes can.
2.4 Scala’s Class Hierarchy
2.5 The Nothing Type
Nothing is at the bottom of Scala’s type hierarchy. It is a subtype of every other type.
There is no value of type
Why is that useful?
- To signal abnormal termination
- As an element type of empty collections (see next session)
Scala’s exception handling is similar to Java’s.
aborts evaluation with the exception
The type of this expression is
2.7 The Null Type
Every reference class type also has
null as a value.
The type of
Null is a subtype of every class that inherits from
Object ; it is incompatible with subtypes of
A fundamental data structure in many functional languages is the immutable linked list.
It is constructed form two building blocks:
Nil, the empty list
Cons, a cell containing an element and the remainder of the list.
Examples for Cons-Lists
3.2 Cons-Lists in Scala
Here’s an outline of a class hierarchy that represents lists of integers in this fashion:
A list is either
- an empty list
new Nil, or
- a list
new Cons(x, xs)consisting of a head element
xand a list
Note the abbreviation
(val head: Int, val tail: IntList) in the definition of
This defines at the same time parameters and fields of a class.
It is equivalent to:
_tail are otherwise unused names.
It seems too narrow to define only lists with
We’d need another class hierarchy for Double list, and so on, one for each possible element type.
We can generalize the definition using a typing parameter:
Type parameters are written in square brackets, e.g. [T].
Complete Definition of List
Like classes, functions can have type parameters.
For instance, here is a function that creates a list consisting of a single element.
We can then write:
In fact, the Scala compiler can usually deduce the correct type parameters from the value arguments of a function call.
Types and Evaluation
Type parameters do not affect evaluation in Scala.
We can assume that all type parameters and type arguments are removed before evaluating the program.
This is also called type erasure.
Languages that use type erasure include Java, Scala, Haskell, ML, OCaml.
Some other languages keeps the type parameters around at run time, these include C++, C#, F#.
Polymorphism means that a function type comes “in many forms”.
In programming it means that
- the function can be applied to arguments of many types, or
- the type can have instances of many types
We have seen two principal forms of polymorphism:
- subtyping: instances of a subclass can be passed to a base class
- generics: instances of a function or class are created by type parameterization.
Wirte a function
nth that takes an integer n and a list and selects the n’th element of the list.
Elements are numbered from 0.
If index is outside the range from 0 up the length of the list minus one, a
IndexOutOfBoundsException should be thrown.