Scala

Scala Traits

Using interfaces is a very important functionality of object-oriented programming. Scala is such a versatile language that it allows you to achieve the same functionality by using traits. Therefore, this guide will be dedicated to using traits in the Scala programming language in Ubuntu 20.04.

What is the Purpose of Traits in the Scala Programming Language?

We have already mentioned that the traits in Scala serve the same purpose as the interfaces in Java. It means that you are allowed to create a generic interface using Scala traits, and your classes will be allowed to extend this trait by implementing the desired methods. Moreover, an interesting thing about the Scala traits is that you are free to use both the abstract and the non-abstract methods within a trait. However, if you use a non-abstract method within a trait, you will not need to provide its implementation within the class that will extend this trait.

Using the Traits in Scala:

For making good use of the traits in Scala in Ubuntu 20.04, you will have to read through the following three examples:

Example # 1: Using Traits with Only Abstract Methods in Scala:

In the Scala programming language, we can have both the abstract and non-abstract methods within a trait. However, in this example, we will just have abstract methods within our defined trait and see how traits work in the Scala programming language. You need to take a look at the code shown below to understand this:

We have first defined a trait named “Category” in this Scala code. Within this trait, we have declared two abstract methods with the help of the “def” keyword with the names “employee” and “employee_age”. Therefore since these are abstract methods, we will implement them within the class that will extend this trait. Then, we have designed a class named “Employee” and have used the “extends” keyword for extending the trait created above from this class. After that, we have first implemented the abstract methods of our trait within this class. Then, we have created another method named “employee_name” within this class.

Finally, we have used the “object” keyword for creating our driver class named “Trait”. Within this class, we have our “main()” function in which we have created a new instance of our “Employee” class named “emp”. Then, we have successively called all the functions of this class (the ones defined within the class and also the ones extended from the trait) by using the instance of this class.

This Scala script can be compiled by using the command given below:

$ scalac Trait.Scala

After a successful compilation, this script can be executed with the following command:

$ scala Trait

You can witness from the output shown below that we have successfully implemented a class that extends from a Scala trait and its output turned out to be exactly as intended.

Example # 2: Using Traits with Both the Abstract and Non-Abstract Methods in Scala:

In this example, we will be using both the abstract and the non-abstract methods in Scala. It means that we will implement some methods within the trait and will implement the others within the class that will extend this trait. In this way, we want to figure out what impact does it leave on our output. Therefore, we will be using the same example that we have implemented above as a baseline with slight modifications. The exact Scala program is as follows:

The difference between our first and second examples is that in this one, we have implemented the “employee_age()” method within our trait. Hence, it became a non-abstract method. On the other hand, we have implemented the “employee()” method within our class that extended our trait. In other words, we have only defined the signature of this function within our trait which made it an abstract function. The rest of the code is the same as our first example.

Now, to see whether this modification left any impact on our output or not, we compiled and executed this script. You can take a look at the output shown below and confirm that this output is the same as our first example. It implies that no matter if you use the abstract or non-abstract methods within Scala traits, your output will remain the same.

Example # 3: Using Multiple Traits in Scala:

A Scala class can also extend multiple traits at a time. The following example depicts this:

Again, we have used the same example as a baseline. The only difference between our second and third examples is that in this one, we have created another trait named “Qualification” and have implemented a non-abstract method within this trait. Then, while extending our class from these traits, we have used the “extends” keyword with the first trait and have used the “with” keyword in between the names of the two traits. Similarly, if you have more than two traits to extend your class from, you will separate the names of all the traits by using the “with” keyword. In our “main()” function, we have also called the function of the new trait that we have created in this example.

The image below presents the output of this Scala script, which confirms that a class can extend multiple traits simultaneously.

Conclusion:

With the help of this tutorial, we wanted to give you a brief overview of the working of the traits in Scala in Ubuntu 20.04. For that, we first shared with you the main purpose of using the traits in Scala by highlighting their close resemblance with the interfaces in Java. Then, we shared how these traits can be used with the abstract and non-abstract methods. Finally, we discussed three examples that used Scala traits effectively. We have tried to keep these examples extremely easy and simple so that you can quickly grab the notion of using Scala traits.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.