What is Scala Trait Mixins – Top Examples of Scala Mixins

1. Objective

After study Scala Trait Tutorial, let’s move towards Scala Trait Mixins. Here, we will discuss what is Scala Mixins Trait and examples of Scala Mixins.

So, let’s start Scala Trait Mixins.

What is Scala Trait Mixins - Top Examples of Scala Mixins

What is Scala Trait Mixins – Top Examples of Scala Mixins

2. What is Scala Trait Mixins?

It is possible to extend any number of Scala traits with a class or with an abstract class. We can extend traits, combinations of traits and classes, or combinations of traits and abstract classes. But to avoid an error from the compiler, we must maintain an order of the mixins.
Let’s discuss Scala Inheritance – Syntax, Example & Types of Inheritance in Scala

Scala Quiz

3. Example of Scala Mixins

Here, we are going to study 3 different examples of Scala Trait Mixins, cover all kind of conditions, so let’s discuss them one by one:

a. Example – 1

Let’s try extending a trait and an abstract class together. As seen before, we use the ‘with’ keyword with this.

scala> trait Greeting{
| def greet()
| }
defined trait Greeting
scala> abstract class Welcome{
| def welcome()
| }
defined class Welcome
scala> class MyClass extends Greeting with Welcome{
| def greet(){
| println("Hello")
| }
| def welcome(){
| println("Welcome")
| }
| }
<console>:14: error: class Welcome needs to be a trait to be mixed in
class MyClass extends Greeting with Welcome{
^

Since we do not maintain the order of the Scala mixins, the compiler throws us an error.
Do You Know About Scala Exceptions and Exception Handling

b. Example – 2

You should first extend any class or abstract class, and then extend any existing Scala traits.

scala> trait Greeting{
| def greet()
| }
defined trait Greeting
scala> abstract class Welcome{
| def welcome()
| }
defined class Welcome
scala> class MyClass extends Welcome with Greeting{
| def greet(){
| println("Hello")
| }
| def welcome(){
| println("Welcome")
| }
| }
defined class MyClass
scala> var m=new MyClass()
m: MyClass = MyClass@47f71f50
scala> m.greet()
Hello
scala> m.welcome()
Welcome

Let’s learn Scala Case Class and to Create Scala Object

c. Example – 3

Let’s try extending only Scala trait.

scala> trait Greeting{
| def greet()
| }
defined trait Greeting
scala> abstract class Welcome{
| def welcome()
| }
defined class Welcome
scala> class MyClass extends Welcome{
| def greet(){
| println("Hello")
| }
| def welcome(){
| println("Welcome")
| }
| }
defined class MyClass
scala> var m=new MyClass() with Greeting
m: MyClass with Greeting = $anon$1@43cb7e55
scala> m.greet()
Hello
scala> m.welcome()
Welcome

As you can see, here, we use the ‘with’ keyword while creating the object.

So, this was all about Scala Trait Mixins. Hope you like our explanation.

4. Trait Mixins –  Conclusion

Hence, in this Trait Mixins Tutorial, we study what is Scala Mixins Traits with examples. Furthermore, if have any query regarding Scala Trait Mixins, feel free to ask in the comment section.

Keep Learning, Keep Visiting!

For Future Study- Scala String Interpolation

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.