Scala | Trait Mixins - GeeksforGeeks

15 Apr, 2019
We can extend several number of scala traits with a class or an abstract class that is known to be trait Mixins. It is worth knowing that only traits or blend of traits and class or blend of traits and abstract class can be extended by us. It is even compulsory here to maintain the sequence of trait Mixins or else the compiler will throw an error.Note:
The Mixins traits are utilized in composing a class.A Class can hardly have a single super-class, but it can have numerous trait Mixins. The super-class and the trait Mixins might have identical super-types.
The Mixins traits are utilized in composing a class.
A Class can hardly have a single super-class, but it can have numerous trait Mixins. The super-class and the trait Mixins might have identical super-types.
Now, lets see some examples.
Extending abstract class with traitExample :// Scala program of trait Mixins  // Trait structuretrait Display{       def Display() }   // An abstract class structureabstract class Show{     def Show() }   // Extending abstract class with// traitclass CS extends Show with Display{          // Defining abstract class    // method    def Display()    {             // Displays output        println("GeeksforGeeks")     }           // Defining trait method    def Show()    {                                             // Displays output         println("CS_portal")     } }  // Creating objectobject GfG{     // Main method    def main(args:Array[String])    {                   // Creating object of class CS        val x = new CS()                   // Calling abstract method        x.Display()                   // Calling trait method        x.Show()     } } Output:GeeksforGeeks
CS_portal
Here, the correct order of Mixins is that, we need to extend any class or abstract class first and then extend any trait by using a keyword with.
// Scala program of trait Mixins  // Trait structuretrait Display{       def Display() }   // An abstract class structureabstract class Show{     def Show() }   // Extending abstract class with// traitclass CS extends Show with Display{          // Defining abstract class    // method    def Display()    {             // Displays output        println("GeeksforGeeks")     }           // Defining trait method    def Show()    {                                             // Displays output         println("CS_portal")     } }  // Creating objectobject GfG{     // Main method    def main(args:Array[String])    {                   // Creating object of class CS        val x = new CS()                   // Calling abstract method        x.Display()                   // Calling trait method        x.Show()     } } 
GeeksforGeeks
CS_portal

Here, the correct order of Mixins is that, we need to extend any class or abstract class first and then extend any trait by using a keyword with.
Extending abstract class without traitExample :// Scala program of trait Mixins  // Trait structuretrait Text{     def txt() }  // An abstract class structureabstract class LowerCase{     def lowerCase() }   // Extending abstract class // without traitclass Myclass extends LowerCase{          // Defining abstract class    // method    def lowerCase()    {         val y = "GEEKSFORGEEKS"                  // Displays output        println(y.toLowerCase())     }           // Defining trait method    def txt()    {                                                   // Displays output         println("I like GeeksforGeeks")     } }   // Creating objectobject GfG{     // Main method    def main(args:Array[String])    {                   // Creating object of 'Myclass'        // with trait 'Text'        val x = new Myclass() with Text                  // Calling abstract method        x.lowerCase()                   // Calling trait method        x.txt()     } } Output:geeksforgeeks
I like GeeksforGeeks
Thus, from this example we can say that the trait can even be extended while creating object.Note: If we extend a trait first and then the abstract class then the compiler will throw an error, as that is not the correct order of trait Mixins.
// Scala program of trait Mixins  // Trait structuretrait Text{     def txt() }  // An abstract class structureabstract class LowerCase{     def lowerCase() }   // Extending abstract class // without traitclass Myclass extends LowerCase{          // Defining abstract class    // method    def lowerCase()    {         val y = "GEEKSFORGEEKS"                  // Displays output        println(y.toLowerCase())     }           // Defining trait method    def txt()    {                                                   // Displays output         println("I like GeeksforGeeks")     } }   // Creating objectobject GfG{     // Main method    def main(args:Array[String])    {                   // Creating object of 'Myclass'        // with trait 'Text'        val x = new Myclass() with Text                  // Calling abstract method        x.lowerCase()                   // Calling trait method        x.txt()     } } 
geeksforgeeks
I like GeeksforGeeks

Thus, from this example we can say that the trait can even be extended while creating object.Note: If we extend a trait first and then the abstract class then the compiler will throw an error, as that is not the correct order of trait Mixins.
Picked
Scala
scala-traits
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
For Loop in Scala
Scala | flatMap Method
Scala | map() method
Scala List filter() method with example
Scala | reduce() Function
String concatenation in Scala
Type Casting in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala List contains() method with example
Scala String substring() method with example