Thursday, September 15, 2016

Class Syntax : Example 3 : Auxiliary Consructor

#143
scala> class Subject(name: String, mark: Int) {
     |    //Specify Precondition for the Primary Constructor
     |    require(mark >= 0)
     |    // An Auxiliary Constructor
     |    def this(name: String) = {
     |       // First Line should always be Primary Constructor or
     |       // another Auxiliary Constructor
     |       this(name, 50) //Here we are calling the Primary Consructor
     |       println("Inside Auxiliary Consructor...")
     |    }
     |    // Override an existing Method
     |    override def toString() =
     |                   name + ", " + mark
     | }
defined class Subject

scala>

scala> val subject = new Subject("Physics")
Inside Auxiliary Consructor...
subject: Subject = Physics, 50