Creating and Handling Classes and Hierarchies in Scala
If you are building software and need to model the entities that will be part of the program you are developing, you should consider using classes. Classes are concepts that refer to the OOP (Object Oriented Programming) but can be also used and integrated with functional programming.
A class represents an entity and models a set of fields (mostly referenced as the state), and behavior (which is a set of methods). After having defined a class, to create and manage the entities it represents, you need to instantiate it by creating an object of that class. Each defined class also creates a type, and the objects created will be of that specific type.
Different classes with common parts can be put in a hierarchy. If you build a hierarchy, you will have top-classes and sub-classes. A top-class represents a generic class, a sub-class represents a more specialized class (a subclass inherits from a top-class).
In Scala, you can define classes and hierarchies. Scala only supports single inheritance, which means that you can only inherit from a single top-class. Scala gives you the possibility to create abstract classes: classes that could have abstract methods (methods with no implementation). Abstract classes can't be instantiated, so you can't create objects from them. If you want to cheat the single-inheritance model, you can consider the Traits. A trait is a class that is like an abstract class, but you can extend more than one trait from a class.
In this lab, you will define and use classes, abstract classes, and traits, and you will put them in a hierarchy.
Upon completion of this beginner level lab, you will be able to:
- Create classes, abstract classes, and traits
- Understand when and where use the different types of classes
This lab is intended for:
- Software engineers who want to leverage the OOP possibilities offered by Scala
- Developers who want to use the functional paradigm and integrate it with the OOP one
To get the most out of this lab, you should have basic knowledge of Scala. To achieve this, we suggest taking the following labs:
April 20th, 2023 - Updated theia to enable autosave
Stefano studies Computer Science and is passionate about technology. He loves working with Cloud services and learning all the best practices for them. Google Cloud Platform and Amazon Web Services are the cloud providers he prefers. He is a Google Cloud Certified Associate Cloud Engineer. Node.js is the programming language he always uses to code. When he's not involved in studying or working, Stefano loves riding his motorbike and exploring new places.