Chapter 4: A Beginner's Tutorial To Using Classes In Scala
In this section, we will go over how to define and use classes in the Scala programming language.
If you recall from the Scala Introduction tutorial, Scala is both an Object Oriented and Functional programming language.
While this chapter is essentially focusing on the Object Oriented aspect of Scala through classes, we will also introduce new concepts such as companion and package objects, case class, type bounds and variance.
Throughout the tutorials we will use the popular IntelliJ IDEA which we've setup in Chapter 1. I hope that by now you are more comfortable using IntelliJ. If not, feel free to review the previous tutorials from Chapter 1!
In this chapter, we will provide tutorials on the topics below. If none of these make any sense right now, that's OK :)
- A simple class
- Companion object
- Companion object as factory
- Singleton object
- Case class
- Type aliasing
- Implicit class
- Package object
- Class inheritance
- Class with type parameter
- Covariance
- Contra-variance
- Upper type bounds
- Lower type bounds
So let's get started!
- Classes And Objects
- Companion Objects
- Companion Objects' Apply Method As A Factory (Class Hierarchy Via Inheritance)
- Values And Fields In Companion Object
- Singleton Object
- Case Class
- Type Alias: Type Aliasing Versus Case Class
- Implicit Class - Extension Methods
- Package Objects
- Extend Abstract Class
- Case Class Inheritance
- Type Class
- Covariance
- Contra-Variance