Creating and Handling Classes and Hierarchies in Scala

Lab Steps

lock
Connecting to the Scala Web IDE
lock
Introducing Abstract Classes and Traits in Scala
lock
Introducing Concrete Classes in Scala

The hands-on lab is part of this learning path

Ready for the real environment experience?

DifficultyIntermediate
Time Limit1h
Students22
Ratings
5/5
starstarstarstarstar

Description

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.

Learning Objectives

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

Intended Audience

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

Prerequisites

To get the most out of this lab, you should have basic knowledge of Scala. To achieve this, we suggest taking the following labs:

About the Author
Students18460
Labs94
Courses2
Learning paths7

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.

Covered Topics