Another Tour of Scala

ScalaClasses

The Gist

Classes and Subclasses give an overview of how to define a simple class in Scala and how to extend it using OO-style inheritance.

My Interpretation

Classes are defined much as they are in Java. Here is a simple class that models a circle.

This is quite different from Java; it’s much more compact, but it also gives you a tiny flavor of the functional side of Scala. Notice how all the method definitions use the equals sign? You are creating a sort-of expression for each one, but it’s evaluated when you call the method.

You can also extend classes much as with Java. Note that when we indicate the class we’re extending, we can pass up constructor values. Also note that superclass methods are called with the super keyword, just as in Java. And yes, I realize this is a horrible example of Object-Oriented design.

This isn’t a comprehensive overview of everything you can do, but gives you some of the flavor of classes in Scala.

My Thoughts on this Feature

Pretty simple. The constructor stuff is great, and the variable/property thing is really nice, too.