Scala | Trait Mixins
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.
Now, lets see some examples.
- Extending abstract class with trait
Example :// Scala program of trait Mixins// Trait structuretraitDisplay{defDisplay()}// An abstract class structureabstractclassShow{defShow()}// Extending abstract class with// traitclassCSextendsShowwithDisplay{// Defining abstract class// methoddefDisplay(){// Displays outputprintln("GeeksforGeeks")}// Defining trait methoddefShow(){// Displays outputprintln("CS_portal")}}// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// Creating object of class CSvalx=newCS()// Calling abstract methodx.Display()// Calling trait methodx.Show()}}chevron_rightfilter_noneOutput: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 trait
Example :// Scala program of trait Mixins// Trait structuretraitText{deftxt()}// An abstract class structureabstractclassLowerCase{deflowerCase()}// Extending abstract class// without traitclassMyclassextendsLowerCase{// Defining abstract class// methoddeflowerCase(){valy="GEEKSFORGEEKS"// Displays outputprintln(y.toLowerCase())}// Defining trait methoddeftxt(){// Displays outputprintln("I like GeeksforGeeks")}}// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// Creating object of 'Myclass'// with trait 'Text'valx=newMyclass()withText// Calling abstract methodx.lowerCase()// Calling trait methodx.txt()}}chevron_rightfilter_noneOutput: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.
Recommended Posts:
- Scala | Trait App
- Scala Trait Traversable | Set-1
- Trait Linearization in Scala
- Scala Trait Traversable | Set-3
- Scala Trait Traversable | Set-4
- Scala Trait Traversable | Set-2
- Scala | Sealed Trait
- Set in Scala | Set-2
- Set in Scala | Set-1
- Scala Map
- Scala | Either
- StringBuilder in Scala
- Scala Map get() method with example
- BitSet in Scala
- while and do while Loop in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



