Scala | Traits
Traits are like interfaces in Java. But they are more powerful than the interface in Java because in the traits you are allowed to implement the members. Traits can have methods(both abstract and non-abstract), and fields as its members.
- Traits are created using trait keywords.
Syntax:trait Trait_Name{ // Fields.. // Methods.. }Example:
// Scala program to illustrate how to// create traits// TraittraitMyTrait{defpetdefpet_color}// MyClass inherits traitclassMyClassextendsMyTrait{// Implementation of methods of MyTraitdefpet(){println("Pet: Dog")}defpet_color(){println("Pet_color: White")}// Class methoddefpet_name(){println("Pet_name: Dollar")}}objectMain{// Main methoddefmain(args:Array[String]){valobj=newMyClass();obj.pet();obj.pet_color();obj.pet_name();}}chevron_rightfilter_noneOutput:
Pet: Dog Pet_color: White Pet_name: Dollar
- In Scala, we are allowed to implement the method(only abstract methods) in traits. If a trait contains method implementation, then the class which extends this trait need not implement the method which already implemented in a trait. As shown in the below example.
Example:
// Scala program to illustrate the concept of// abstract and non-abstract method in Traits// Trait with abstract and non-abstract methodstraitMyTrait{// Abstract methoddefgreeting// Non-abstract methoddeftutorial{println("This is a tutorial"+"of Traits in Scala")}}// MyClass inherits traitclassMyClassextendsMyTrait{// Implementation of abstract method// No need to implement a non-abstract// method because it already implementeddefgreeting(){println("Welcome to GeeksfoGeeks")}}objectMain{// Main methoddefmain(args:Array[String]){valobj=newMyClass();obj.greetingobj.tutorial}}chevron_rightfilter_noneOutput:
Welcome to GeeksfoGeeks This is a tutorial of Traits in Scala
- Traits does not contain constructor parameters.
- When a class inherits one trait, then use extends keyword.
Syntax:class Class_Name extends Trait_Name{ // Code.. } - When a class inherits multiple traits then use extends keyword before the first trait and after that use with keyword before other traits. As shown in the below example.
Syntax:class Class_Name extends Trait_Name1 with Trait_Name2 with Trait_Name3{ // Code.. }Example:
// Scala program to illustrate how// a class inherits multiple traits// Trait 1traitMyTrait1{// Abstract methoddefgreeting}//Trait 2traitMyTrait2{// Non-abstract methoddeftutorial{println("This is a tutorial"+"of Traits in Scala")}}// MyClass inherits multiple traitsclassMyClassextendsMyTrait1withMyTrait2{// Implementation of abstract methoddefgreeting(){println("Welcome to GeeksfoGeeks")}}objectMain{// Main methoddefmain(args:Array[String]){valobj=newMyClass();obj.greetingobj.tutorial}}chevron_rightfilter_noneOutput:
Welcome to GeeksfoGeeks This is a tutorial of Traits in Scala
- An abstract class can also inherit traits by using extends keyword.
Syntax:abstract class Class_name extends Trait_Name{ // code.. } - In Scala, one trait can inherit another trait by using a extends keyword.
Syntax:trait Trait_Name1 extends Trait_Name2{ // Code.. } - Traits support multiple inheritance.
- In Scala, a class can inherit both normal classes or abstract class and traits by using extends keyword before the class name and with keyword before the trait’s name.
Syntax:class Class_Name1 extends Class_Name2 with Trait_Name{ // Code.. } - In Traits, abstract fields are those fields with containing initial value and concrete fields are those fields which contain the initial value. we are allowed to override them in the class which extends trait. If a field is declared using the var keyword, then there is no need to write override keyword when we override them. And if a field is declared using the val keyword, then you must write override keyword when we override them.
Example:// Scala program to illustrate// concrete and abstract fields in traitstraitMyTrait{// Abstract fieldvarvalue:Int// Concrete fieldvarHeight=10valWidth=30}classMyClassextendsMyTrait{// Overriding MyTrait's fieldsvarvalue=12Height=40overridevalWidth=10// Method to display the fieldsdefDisplay(){printf("Value:%d", value);printf("\nHeight:%d",Height);printf("\nWidth:%d", Width);}}objectMain{// Main methoddefmain(args:Array[String]){valobj=newMyClass();obj.Display();}}chevron_rightfilter_noneOutput:
Value:12 Height:40 Width:10
- We can also add traits to an object instance. Or in other words, We can directly add a trait in the object of a class without inheriting that trait into the class. We can add a trait in the object instance by using with keyword.
Syntax:val object_name = new Class_name with Trait_Name;
Example:
// Scala program to illustrate how// to add a trait to an object instanceclassMyClass{}traitMyTrait{println("Welcome to MyTrait");}objectMain{// Main methoddefmain(args:Array[String]){// Here MyTrait is added to the// object instance of MyClassvalobj=newMyClasswithMyTrait;}}chevron_rightfilter_noneOutput:
Welcome to MyTrait

