How to define Auxiliary Constructor in case class in Scala
We have seen case class previously. We had discussed what is case class, how to create object of case class, list of boilerplate code generated for case class. If you don't know case class, I recommend to understand case class first.
Let us revise case class declaration.
The constructor which is not primary constructor, other than primary constructors and overloaded to primary constructor are called auxiliary constructor.
How will you declare Auxiliary(other overloaded) constructors. What is syntax of creating auxiliary constructor.
Please note that constructors in case class are not really constructor. Actually these are apply methods on companion object. Auxiliary constructor in case class are different than regular class.
We can add auxiliary constructors in case class by adding apply() methods in companion object of case class.
Let us create 2 auxiliary constructor in below code -
Let us revise case class declaration.
case class Person(id:Int, name: String)Person class is declared with primary constructor where id and name are parameter. So good so far.
The constructor which is not primary constructor, other than primary constructors and overloaded to primary constructor are called auxiliary constructor.
How will you declare Auxiliary(other overloaded) constructors. What is syntax of creating auxiliary constructor.
Please note that constructors in case class are not really constructor. Actually these are apply methods on companion object. Auxiliary constructor in case class are different than regular class.
We can add auxiliary constructors in case class by adding apply() methods in companion object of case class.
Let us create 2 auxiliary constructor in below code -
case class Person(id:Int, name: String) object Person{ def apply(): Person = new Person(1, "EMPTY") def apply(id: Int): Person = new Person(id, "EMPTY") }We have created 2 auxiliary constructor. First does not take any parameter and second take one Int parameter. Both constructor are calling primary constructor.
Now it is time to create object by calling these new constructors.
object TestMe { def main(args: Array[String]): Unit = { val person1 = Person() val person2 = Person(101) println(person1) println(person2) } }Output:
Person(1,EMPTY)
Person(101,EMPTY)
I have read your article throughly and it was awesome content with great example.
ReplyDeleteGerman Classes in Chennai
Big Data Training in Chennai
Android Training in Chennai
Selenium Training in Chennai
JAVA Training in Chennai
Digital Marketing Training in Chennai
German Classes in Adyar
"Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up ..
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
very nice blogs!!! I have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery