| 1. |
How To Define Factory Methods Using Object Keyword In Scala? What Is The Use Of Defining Factory Methods In Object? |
|
Answer» In Scala, we USE ‘object’ keyword to define Factory methods. The main purpose of these Factory methods in Scala is to avoid using ‘new’ keyword. Without using ‘new’ keyword we can create OBJECTS. To define Factory methods: We can use apply method to define Factory methods in Scala. If we have Primary Constructor and Multiple Auxiliary CONSTRUCTORS, then we NEED to define multiple apply methods as shown below. class Person(val FIRSTNAME: String, val middleName: String, val lastName: String){ In Scala, we use ‘object’ keyword to define Factory methods. The main purpose of these Factory methods in Scala is to avoid using ‘new’ keyword. Without using ‘new’ keyword we can create objects. To define Factory methods: We can use apply method to define Factory methods in Scala. If we have Primary Constructor and Multiple Auxiliary constructors, then we need to define multiple apply methods as shown below. class Person(val firstName: String, val middleName: String, val lastName: String){ |
|