The Wayback Machine - https://web.archive.org/web/20240119133214/https://www.geeksforgeeks.org/scala-trait-mixins/
Open In App
Related Articles

Scala | Trait Mixins

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

  1. The Mixins traits are utilized in composing a class.
  2. 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 structure
    trait Display
    { 
      
        def Display() 
    } 
      
    // An abstract class structure
    abstract class Show
    { 
        def Show() 
    } 
      
    // Extending abstract class with
    // trait
    class 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 object
    object 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.

  • Extending abstract class without trait
    Example :




    // Scala program of trait Mixins
      
    // Trait structure
    trait Text
    { 
        def txt() 
    }
      
    // An abstract class structure
    abstract class LowerCase
    { 
        def lowerCase() 
    } 
      
    // Extending abstract class 
    // without trait
    class 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 object
    object 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.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 15 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials