7 Classes and Subclasses to represent the world
After reading this lesson, you will be able to:
- Represent real-worlds elements and their interactions
- Design class-subclass relations
After discovering the basics of functions in Scala, you’ll learn about classes in this lesson. The concept of class is fundamental to the object-oriented paradigm, and it allows you to represent elements and interactions from the real world. Classes, subclasses, and abstract classes enable you to group entities that have common shapes and behaviors. If you are familiar with other object-oriented programming languages, you’ll find similarities with Scala’s concept of class. Table 7.1 recaps the different types of classes Scala can offer. In the capstone, you’ll use a class to represent your vending machine.
Table 7.1: Summary of the types of classes in Scala. They match the definitions of most other object-oriented languages.
Term |
Definition |
Class |
A representation of elements of the same kind from the real world. All its functions have an implementation. |
Subclass |
A class that inherits behaviors from another class, called superclass. In Scala, a class can only up have up to one superclass. |
Superclass |
A class whose methods are inherited by one or more classes, called subclasses. |
Abstract Class |
A class in which one or more methods are abstract (i.e., they do not have an implementation). |