Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Scala Traits


Traits are abstract classes that are meant to be added to some other class. A trait adds some methods or fields to an unknown parent class. It encapsulates field and method definitions which can be reused by adding them into classes. Unlike class inheritance in which each class must inherit from just one superclass whereas a class can mix in any number of traits. A trait defines the object types by specifying the signature of the supported methods.


trait IntegerSet {

def incl(x: Int): IntSet

def contains(x: Int): Boolean


class EmptySet extends IntegerSet {

def contains(x: Int): Boolean = false

def incl(x: Int): IntSet = new NonEmptySet(x, new EmptySet, new EmptySet)



One trait can also extend another trait like as follows:


trait Almirah {

def canOpen(p: Person) : Boolean =

return true

def canPass(p: Person) : Boolean =

return true


trait Locked extends Almirah {

override def canOpen(p: Person): Boolean = {

if (!p.hasItem(theKey)) {

println("You don't have the Key")

return false


println("Using key...")

return super.canOpen(p)



"0 Responses on Scala Traits"

    Training in Cities

    Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

    Sales Offer

    • To avail this offer, enroll before 25th October 2016.
    • This offer cannot be combined with any other offer.
    • This offer is valid on selected courses only.
    • Please use coupon codes mentioned below to avail the offer


    Sign Up or Login to view the Free Scala Traits.