How to use a Scala trait like an abstract class

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 8.3, “How to use a Scala trait like an abstract class.”


You want to use a trait as something like an abstract class in Java.


Define methods in your trait just like regular Scala methods. In the class that extends the trait, you can override those methods or use them as they are defined in the trait.

How to use abstract and concrete fields in Scala traits

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.2, “How to use abstract and concrete fields in Scala traits.”


You want to put abstract or concrete fields in your traits so they are declared in one place and available to all types that implement the trait.

How to use a Scala trait as an interface (like a Java interface)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.1, “How to use a Scala trait as an interface.”


You’re used to creating interfaces in other languages like Java and want to create something like that in Scala.


You can use a Scala trait just like a Java interface. As with interfaces, just declare the methods in your trait that you want extending classes to implement:

Scala trait examples and syntax

This page contains a collection of Scala trait examples. I created many of these examples when I was writing the Scala Cookbook. Unlike the Cookbook, where I explain these examples in great detail, on this page I’m just sharing many of the examples so you can use this as a trait reference page. (The Cookbook actually contains more examples than this page.)

Without any more introduction, here are the examples.

A Java deep clone (deep copy) example

While interviewing for computer programming positions in Boulder, Colorado, I found that most interviewers ask questions about Java serialization. After being asked about serialization for the third time recently, I suddenly remembered an old Java deep clone hack that takes advantage of serialization.

The basic idea is this:

The Strategy Design Pattern in Java

Summary: A discussion of the Strategy Design Pattern using Java source code examples.

The Strategy Design Pattern consists of a number of related algorithms encapsulated in a driver class often named Context. A user or a client program typically selects the algorithm they want to use, although the Context class may also select the algorithm automatically.

The intent of the Strategy Pattern is to make the algorithms easily interchangeable, and provide a means to choose the appropriate algorithm at a particular time.

A Java “Extract Interface” refactoring example

Java Refactoring FAQ: Can you provide an example of the Extract Interface refactoring process?

While working on a Java Swing development project recently, I had written a couple of controllers (as in controllers from the Model/View/Controller pattern), and I was about to write some more, when I realized that if I refactored my Java source code I would have a much better design -- source code I code more easily maintain.

The pattern I saw repeated in my Java controller classes was that they all had similar method names, something like this:

Nagios web application screenshots

Sorry folks, I hoped to be able to release the source code I created for a Nagios Web Interface, but after starting on it a few hours ago, I'm giving up on this project. The code works just fine, as you can see from the screenshots below.

The problem is the amount of work it will take to make this code "clean" for public release. It currently has my client name all over it, and removing all those references is just going to take too long. (Unless of course an angel investor wants to contribute a few week's pay into my PayPal account.)