Introducing Concrete Classes 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

Concrete classes are core components when you need to complete a hierarchy. They represent entities that can be instantiated by creating objects. Instead of abstract classes, the concrete classes can't contain any abstract element, all of them need to be defined if inherited from a top-class. A class can inherit from only one class (both abstract or concrete), and from different traits.

In this lab step, you will create a concrete class and it will inherit from the abstract class and trait you created before.

 

Instructions

1. Create a new concrete class called MaterializedClass by inserting the following code snippet outside the Main object:

Copy code
class MaterializedClass (val x: Int, val y: Int, val z: Int) extends Abstract(x, y) with Trait {

}

The class has three class parameters that are automatically set as class fields because you inserted the val keyword (you can use both x, y, z as class parameters and class fields).The class also inherits from the Abstract class, and from the Trait.

If you want to inherit from a class, you need to use the extends keyword; if you want to inherit from a trait, you need to use the with keyword.

 

2. Define the absMethod and the traitSecondMethod methods that you inherited from the Abstract class and the Trait (you need to define that because these methods are abstract):

Copy code
def absMethod () = println(this.y)
def traitSecondMethod () = println("Second method of the Trait implemented by the class")

 

5. Enter the following code snippet inside the main entry point function that creates a MaterializedClass object and prints out the result of two methods:

Copy code
val firstInstance: MaterializedClass = new MaterializedClass(1, 2, 3)

firstInstance.printMethod()
firstInstance.traitFirstMethod()

The complete MaterializedClass class and Main object will look like this:

Copy code
class MaterializedClass (val x: Int, val y: Int, val z: Int) extends Abstract(x, y) with Trait {
    def absMethod () = println(this.y)
    def traitSecondMethod () = println("Second method of the Trait implemented by the class")
}

object Main {
    def main(args: Array[String]) = {
        val firstInstance: MaterializedClass = new MaterializedClass(1, 2, 3)

        firstInstance.printMethod()
        firstInstance.traitFirstMethod()
    }
}

When you need to create a new object, you need to use the new keyword followed by the sign of the constructor you want to use.

 

6. Save the content of the file by clicking on File -> Save.

 

7. Expand the Terminal dropdown, and click on New Terminal. A terminal window will open:

alt

 

8. Execute the following command:

Copy code
1
scalac Main.scala

The scalac tool is the compiler invoker. That way you will compile the scala sources you specify.

Warning: If you reach out a warning message, just proceed with the lab, it doesn't affect the expected lab result.

 

9. Execute the following command to launch the Scala source you just compiled:

Copy code
1
scala Main

alt

The first method called is the printMethod: it's not defined in the MaterializedClass, but in the Abstract class, so this one is called because it's been inherited. The second method called is the traitFirstMethod: in this case the method is not defined in the MaterializedClass too, but in the Trait, so this one is called because it's been inherited.

 

10. Try to override the printMethod in the MaterielizedClass by inserting this code snippet in the MaterializedClass body:

Copy code
override def printMethod () = println(this.z)

If you need to override a method you have inherited from a class or a trait, you need to use the override keyword.

 

11. Save the content of the file by clicking on File -> Save.

 

12. Compile again the Main.scala file and then execute it with the following commands:

Copy code
scalac Main.scala
scala Main

alt

As you can see, the output of the printMethod is not 1 anymore, but it's 3. That's because the method called is the one you just overridden in the MaterializedClass body.

The complete hierarchy you defined is the following one:

alt

 

Summary

In this lab step, you created a concrete class and it inherited from the abstract class and trait you created before.

Validation checks
1Checks
Created the MaterializedClass class and Overridden the printMethod method

Check whether the MaterializedClass has been created and the printMethod method overridden.

Scala