Traits

A trait is like an interface with a partial implementation. Scala traits consist of method and field definition that can be reused by mixing classes. The classes can mix any number of traits. 

  • In scala, trait is a collection of abstract and non-abstract methods. 
  • You can create trait that can have all abstract methods or some abstract and some non-abstract methods. 
  • A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait. 
  • Any variable which is declared by using val or var but not initialized is considered abstract.
  • Traits are compiled into Java interfaces with corresponding implementation classes that hold any methods implemented in the traits.
Trait With abstract method
trait MyTrait
{
def pet
def pet_color
}

class MyClass extends MyTrait
{

def pet()
{
println("Pet: Dog")
}

def pet_color()
{
println("Pet_color: White")
}

def pet_name()
{
println("Pet_name: Dollar")
}
}

object Main
{

def main(args: Array[String])
{
val obj = new MyClass();
obj.pet();
obj.pet_color();
obj.pet_name();
}

In Scala we are allowed to implement the method (only abstract methods) in traits.if trait contain method implementation,then the class which extends this trait need not implement the method which already implemented in a trait.

Trait with abstract and non-abstract methods.

trait MyTrait
{
    def greeting
    def tutorial
    {
        println("This is a tutorial" + "of Traits in Scala")
    }
}

class MyClass extends MyTrait
{
    // Implementation of abstract method
    // No need to implement a non-abstract
    // method because it already implemented
    def greeting()
    {
        println("Trait with abstract and non abstract method")
    }
}

object Main
{
    def main(args: Array[String])
    {
        val obj = new MyClass();
        obj.greeting
        obj.tutorial
    }
}
  • Traits does not contain constructor parameters.
  • Where a class inherits one trait then use extends keywords.
  • When class inherits multiple  traits then use extend keywords before the first trait and after that use with keyword before other traits.
class Class_Name extends Trait_Name1 with Trait_Name2 with Trait_Name3{
// Code..
}

trait myTrait1
{   
    def greeting
}
trait myTrait2
{   
    def tutorial
    {

    }
}

class myclass extends myTrait1 with myTrait2
{
 // Implementation of abstract method
    def greeting()
    {
        println("Welcome to Scala Tutorials")
    }
}

object traits1 {
def main(args: Array[String])
 {
  var obj= new myclass();
      obj.greeting
      obj.tutorial
}
}

If a class extends a trait but does not implement the members declared in that trait, it must be declared abstract. Let's see an example.

    trait Printable{ 
        def print() 
    } 
     
    abstract class A4 extends Printable{            // Must declared as abstract class 
       def printA4(){ 
           println("Hello, this is A4 Sheet") 
       } 
    } 

    class A5 extends A4{
      def print{
        println("hello")
      }
     }
   
        object MainObject{
        def main(args:Array[String]){
         val obj=new A5
           obj.print
           obj.printA4
        }
    }

Scala Trait Example: Implementing Multiple Traits in a Class

If a class implements multiple traits, it will extend the first trait, class, abstract class. with keyword is used to extend rest of the traits.

You can achieve multiple inheritances by using trait.

    trait Printable{ 
        def print() 
    } 
     
    trait Showable{ 
       def show() 
    } 
     
    class A6 extends Printable with Showable{ 
        def print(){ 
            println("This is printable") 
        } 
        def show(){ 
            println("This is showable"); 
        } 
    } 
     
    object MainObject{ 
        def main(args:Array[String]){ 
            var a = new A6() 
            a.print() 
            a.show() 
        } 
    } 

 

No comments:

Post a Comment