O'Reilly logo
live online training icon Live Online training

Scala core programming: Methods, classes, and traits

Daniel Hinojosa

Scala is a dynamic, general-purpose programming language for the Java Virtual Machine (JVM) used in everything from data science to distributed computing. Join expert Daniel Hinojosa for a deep dive into core Scala programming topics, including methods, classes, and traits. This goes further than the basics. You'll dig into recursion, partially applied functions, by-name parameters, repeated parameters, default parameters, and how to turn a method into a function. You'll go further into classes too, exploring how to subclass, abstract classes, generic classes, and preconditions. Daniel then details traits that offer multiple benefits: one as an abstraction, the other to mix in functionality at both compile time and at runtime. By the time you're through, you'll have mastered best practices and be able to write Scala code that is elegant, scalable, and fully interoperable with Java.

This class is a great follow-up if you’ve taken Daniel’s Learn the basics of Scala in 3 hours and Setting up Scala projects courses or if you already understand concepts such as manipulating variables, classes, and functions.

What you'll learn-and how you can apply it

By the end of this live online course, you’ll understand:

  • How to use recursion in methods
  • How to use prerequisites and class constructors
  • How to use traits to mix in functionality

And you’ll be able to:

  • Create and set up basic projects
  • Determine when and how to use generic types
  • Avoid the "diamond of death" when using inheritance
  • Use self types to guide how to inherit classes and traits

This training course is for you because...

  • You're already using Scala, and you want to know more about classes, abstract classes, and traits to better design applications and choose the right construct for the right reason.
  • You want to understand object-oriented programming in Scala and compare object orientation in Scala to the language you use.
  • You want to explore Scala in more detail.

Prerequisites

  • A basic understanding of Scala (equivalent to Learn the basics of Scala in 3 hours)

Materials or downloads needed in advance:

  • A machine with the latest JDK 8, JDK 9, or JDK 10, Scala, the latest IntelliJ or Eclipse with the Scala and Scala-IDE plug-ins, and sbt installed and set up

Recommended preparation:

Recommended follow-up:

About your instructor

  • Daniel Hinojosa is an instructor, speaker, recent author, programmer, and consultant with over 20 years of experience working with private, educational, and government institutions. He is also currently a speaker on the No Fluff Just Stuff tour. Daniel loves JVM languages like Java, Groovy, and Scala, but he also dabbles with non-JVM languages like Haskell, Ruby, Python, Lisp, C, and C++. He is an avid Pomodoro Technique practitioner and attempts to learn a new programming language every year. Daniel is the author of Testing in Scala and the video Beginning Scala Programming as well as the teacher of a line of training courses, all for O’Reilly. In his downtime, he enjoys reading, swimming, playing with Legos, and cooking.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Scala methods (40 minutes)

  • Lecture and hands-on exercises: Embedding methods; recursion; repeated parameters; default parameters

Break (10 minutes)

Scala classes (65 minutes)

  • Lecture and hands-on exercises: Scala classes overview; case classes overview; preconditions; subclassing; abstract classes; basic generics

Break (10 minutes)

Determining instances (15 minutes)

  • Lecture and hands-on exercises: isInstanceOf; asInstanceOf

Scala traits (40 minutes)

  • Lecture and hands-on exercises: Interface replacement; abstract class replacement; mixins; avoiding the "diamond of death"; stacking traits; self types