7 Classes and subclasses to represent the world

 

After reading this lesson, you will be able to

  • Represent real-world elements and their interactions
  • Design class-subclass relations

Now that you know 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, which 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 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 may be abstract (i.e., they do not have an implementation).

  

7.1 Class

7.2 Subclass

7.3 Abstract class

Summary

Answers to quick checks