InterviewSolution
Saved Bookmarks
| 1. |
How can you define an auxiliary constructor for a class? |
|
Answer» Scala PERMITS defining the auxiliary constructors of a class using this keyword. If we define one or more than one methods in the class body, they BECOME the auxiliary constructors. The first statement of an auxiliary CONSTRUCTOR must be either a call to the primary constructor or any other auxiliary constructor. For example, class Customer(val ID:Int, val name:STRING, val age:Int) { def this(id:Int, name:String, dob:LocalDate) = { this(id, name, Period.between(dob, LocalDate.now).getYears) } } |
|