Scala Trait – Multiple Class and Examples with New Experiments
Scala course with real-time projects Start Now!!
In our last Scala Tutorial, we studied Scala Annotations and in this Scala Trait tutorial, we will learn about what is a trait in Scala Programming Language. Moreover, we will discuss syntax and examples of Scala traits. Along with this, we will look at Scala traits with abstract and non-abstract methods. At last, we will cover override with a trait and multiple traits in a Class.
So, let’s start Scala Trait.
Introduction to Scala Trait
Traits in Scala are like partially implemented interfaces. It may contain abstract and non-abstract methods. It may be that all methods are abstract, but it should have at least one abstract method. Not only are they similar to Java interfaces, Scala compiles them into those with corresponding implementation classes holding any methods implemented in the traits.
You can say that using Scala trait, we can share interfaces and fields between classes. Within trait, we can declare variables and values. When we do not initialize them, they are abstract. In other cases, the implementing class for the trait internally implements them.
Finally, we can never instantiate Scala traits, and it has no parameters, but we can have classes and objects extend it.
Let’s Revise Scala Case Class and Create Scala Object in Detail
A Syntax of Scala Trait
To define trait, we use the ‘trait’ keyword:
scala> trait Footwear
defined trait Footwear
We can also use Scala traits with abstract methods and as generic types:
scala> trait Iterator[A]{ | def hasNext:Boolean | def next():A | }
defined trait Iterator
Now, let’s take an example of a simple trait.
Scala Trait Example
scala> trait Greeting{ | def greet() | } defined trait Greeting scala> class Rendezvous extends Greeting{ | def greet(){ | println("Hello") | } | } defined class Rendezvous scala> var r=new Rendezvous() r: Rendezvous = Rendezvous@4cf5d999 scala> r.greet()
Hello
This was a simple trait with one abstract method. Now, let’s take another example.
Read About Scala Final – Variable, Method & Class | Scala This
With Abstract and Non-Abstract Methods
Here, we will see the example of trait with the abstract and non-abstract method.
scala> trait Greeting{ | def greet() | def show(){ //non-abstract | println("This is the out method") | } | } defined trait Greeting scala> class Rendezvous extends Greeting{ | def greet(){ | println("Hello") | } | } defined class Rendezvous scala> var r=new Rendezvous() r: Rendezvous = Rendezvous@4c6fe482 scala> r.greet()
Hello
scala> r.show()
This is the out method
While we cannot extend multiple abstract classes, we can extend multiple traits in Scala.
Multiple Traits in a Class
For a class extending multiple traits in Scala, we use ‘extends’ for one trait and ‘with’ for the rest. Hence, using traits, we can achieve multiple inheritances.
scala> trait A{ | def showA() | } defined trait A scala> trait B{ | def showB() | } defined trait B scala> class MyClass extends A with B{ | def showA(){ | print("A") | } | def showB(){ | print("B") | } | } defined class MyClass scala> var m=new MyClass() m: MyClass = MyClass@3a4181ba scala> m.showA()Â A Scala> m.showB() B
Let’s Explore Scala Exceptions and Exception Handling
An Experiment with Trait in Scala
What happens when we do not implement all members of Scala trait in a class that extends it? Let’s find out.
scala> trait Greeting{Scala Trait Mixins Scala Trait Mixins | def greet() | } defined trait Greeting scala> class Rendezvous extends Greeting{ | def show(){ | println("This is a rendezvous") | } | } <console>:12: error: class Rendezvous needs to be abstract, since method greet in trait Greeting of type ()Unit is not defined class Rendezvous extends Greeting{ ^
Since we did not implement greet() from the trait Greeting, we must declare it abstract. This works:
scala> trait Greeting{ | def greet() | } defined trait Greeting scala> abstract class Rendezvous extends Greeting{ | def show(){ | println("This is a rendezvous") | } | } defined class Rendezvous
Do You Know About Scala List with Examples
Override with Scala Trait
To implement abstract members of a trait, we can use the ‘override’ keyword.
scala> trait Iterator[A]{ | def hasNext:Boolean | def next():A | } defined trait Iterator scala> class IntIterator(to:Int) extends Iterator[Int]{ | private var current=0 | override def hasNext:Boolean=current<to | override def next():Int={ | if(hasNext){ | val t=current | current+=1 | t | } else 0 | } | } defined class IntIterator scala> val iterator=new IntIterator(10) iterator: IntIterator = IntIterator@5b2728db scala> iterator.next() res6: Int = 0 scala> iterator.next() res7: Int = 1
So, this was all about Scala Trait Tutorial. Hope you like our explanation.
Read About Scala Method Overloading with Example
Conclusion
Hence, in this article, we can say that Scala trait is a partially implemented interface. We can use it to share fields and interfaces between classes. In addition, we got know about example and syntax of Scala traits and experiment with Scala trait.
In conclusion, we cover the multiple Scala traits in a class and use abstract and non-abstract methods with scala trait. Furthermore, if you have any query, feel free to ask in the comment section.
Related Topic-Â 13 Simple Methods to Call Option in Scala
For reference
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google