Introducing Abstract Classes and Traits 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
Need help? Contact our support team

Here you can find the instructions for this specific Lab Step.

If you are ready for a real environment experience please start the Lab. Keep in mind that you'll need to start from the first step.

Introduction

Abstract classes and Traits are very important components in Scala when it comes to the need to build hierarchies. They are commonly used as base entities that can contain both abstract and concrete methods. The difference between an abstract class and a trait is that an abstract class is like a concrete class but it can't be instantiated; a trait is more like an interface, so it can't have constructors and/or fields.

In Scala you can simulate multiple inheritance by integrating multiple traits. You can't extend multiple classes.

In this lab step, you will better understand abstract classes and traits.

 

Instructions

1. Click on the Explorer icon, and open the Main.scala file:

 alt

 

2. Create a new abstract class called Abstract by inserting the following code snippet outside the Main object:

Copy code
abstract class Abstract (x: Int, y: Int) {

}

The abstract keyword is needed to create an abstract entity (a class or a method). The Abstract class has two class arguments. They can be only used and referred to within the class body; they are not class fields.

The constructor is represented by the list of class parameters.

 

3. Enter the following code snippets inside the Abstract class to define two new class fields:

Copy code
val firstField: Int = x
val secondField: Int = y

These two fields are public (they can be accessed by any other Scala component) by default. You can also specify different access modifiers. For a complete overview of the access modifiers, you can read the following article.

 

4. Enter the following code snippets inside the Abstract class to create two methods:

Copy code
def absMethod ()
def printMethod () = println(this.firstField)

The absMethod is an abstract method because you only specified the definition but not the body; the printMethod is concrete because you also specified the body.

The complete Abstract class will look like this:

Copy code
abstract class Abstract (x: Int, y: Int) {
    val firstField: Int = x
    val secondField: Int = y

    def absMethod ()
    def printMethod () = println(this.firstField)
}

 

5. Create a new trait called Trait by inserting the following code snippet outside the Abstract class:

Copy code
trait Trait {

}

The trait definition is very simple, you only specify the name with no other parts: no arguments are allowed.

 

6. Enter the following code snippets inside the Trait to define two methods:

Copy code
def traitFirstMethod () = println("First method of the Trait")
def traitSecondMethod ()

As you did before, you defined a concrete method (traitFirstMethod), and an abstract method (secondTraitMethod).

The complete Trait will look like this:

Copy code
trait Trait {
    def traitFirstMethod () = println("First method of the Trait")
    def traitSecondMethod ()
}

The hierarchy you just defined has this state:

alt

 

Summary

In this lab step, you will better understand abstract classes and traits.

Validation checks
1Checks
Created the Abstract Class and the Trait

Check whether the Abstract class and the Trait have been created.

Scala