| 1. |
How Do We Declare A Private Primary Constructor In Scala? How Do We Make A Call To A Private Primary Constructor In Scala? |
|
Answer» In Scala, we can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and ADD ‘private’ just after class NAME and before parameter LIST as shown below: class Person private (name: String) As it’s a private constructor, we cannot call it from outside. We should PROVIDE a factory method (that is apply method) as shown above and use that constructor INDIRECTLY. In Scala, we can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and add ‘private’ just after class name and before parameter list as shown below: class Person private (name: String) As it’s a private constructor, we cannot call it from outside. We should provide a factory method (that is apply method) as shown above and use that constructor indirectly. |
|