Scala Triats - Scala

What is Scala - Triats?

A trait encapsulates method and field definitions, that can then be reused by combining them into classes. Unlike class inheritance, in which every class must inherit simply from one superclass, a class can combine in any number of traits.

Traits are usedfor defining object types by specifying the signature of the supported methods. Scala also permits traits to be partly implemented but traits may not have constructor parameters.

A trait definition looks just like a class definition except that it employes the traitkeyword. The below is the basic example syntax of trait.

Syntax

This trait contains two methods isEqual and isNotEqual. Here, we have not mentioned any implementation for isEqual where as another method has its implementation. Child classes extending a trait can mention implementation for the un-implemented methods. So a trait is same as what we have abstract classes in Java.

Let us assume an example of trait Equal consists of two methods isEqual() and isNotEqual(). The trait Equal consists of one implemented method that is isEqual() so when user defined class Point extends the trait Equal, implementation to isEqual() method in Point class should be facilitated.

Here it is needed to know two primary method of Scala, which are employed in the below example.

  • obj.isInstanceOf [Point] To check Type of obj and Point are same are not.
  • obj.asInstanceOf [Point] means exact casting by taking the object obj type and returns the same obj as Point type.

Try the below example program to implement traits.

Example

Save the above program in Demo.scala. The below commands are used for compiling and executing this program.

Command

Output

Value classes and Universal Traits

Value classes are new mechanism in Scala to mitigate allocating runtime objects. It consists of a primary constructor with exactly one val parameter. It consists ofonly methods (def) not permittedvar, val, nested classes, traits, or objects. Value class cannot be extended by other class. This can be done by extending your value class with AnyVal. The typesafety of custom datatypes without the runtime overhead.

Let us consider an examples of value classes Weight, Height, Email, Age, etc. For all these examples it is not needed to allocate memory in the application.

A value class not permittedfor extending traits. For permit value classes to extend traits, universal traits are introduced which extends for Any.

Example

Save the above program in Demo.scala. The below commands are used for compiling and executing this program.

Command

Output

It will give you the hash code of Wrapper class.

When to Use Traits?

There is no firm rule, but here are few guidelines to be taken in to consideration−

  • If the behavior is not be reused, then make it a concrete class. It is not reusable behavior after all.
  • If it may be reused in multiple, unrelated classes, make it a trait. Only traits can be combined into various parts of the class hierarchy.
  • If you wish to inherit from it in Java code, use an abstract class.
  • If you plan to distribute it in compiled form, and you expect outside groups to write classes inheriting from it, you might lean towards employingan abstract class.
  • If efficiency is very primary, lean towards employing a class.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Scala Topics