How to define Auxiliary Constructor in case class in Scala [PDF]

Oct 7, 2017 - How to define Auxiliary Constructor in case class in Scala. We have seen case class previously. We had dis

2 downloads 25 Views 212KB Size

Recommend Stories


Manualul inginerului constructor pdf
Courage doesn't always roar. Sometimes courage is the quiet voice at the end of the day saying, "I will

How to define interface patterns in model-based system engineering
Life isn't about getting and having, it's about giving and being. Kevin Kruse

Parallel Programming in Scala
Your big opportunity may be right where you are now. Napoleon Hill

14 Veicolo in Scala
Come let us be friends for once. Let us make life easy on us. Let us be loved ones and lovers. The earth

in der Define-Phase
If you want to become full, let yourself be empty. Lao Tzu

[PDF] How To See In The Spirit
How wonderful it is that nobody need wait a single moment before starting to improve the world. Anne

PDF How to program ESP8266 in Lua
Kindness, like a boomerang, always returns. Unknown

How to Identify 508 Errors in PDF
It always seems impossible until it is done. Nelson Mandela

How Should We Theorize Class Interests in Thinking about [PDF]
Carle, Susan D. (2003) "How Should We Theorize Class Interests in Thinking about Professional Regulation: The Early NAACP as a ...... much like another leading theoretical approach: Parsonian structural- functionalism. .... 1970s, Abel looks for ways

How should we define health?
The greatest of richness is the richness of the soul. Prophet Muhammad (Peace be upon him)

Idea Transcript


Java Gladiator Home

Contact Us

SATURDAY, 7 OCTOBER 2017

ABOUT ME

Prithvipal Singh

How to define Auxiliary Constructor in case class in Scala

Follow

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.

View my complete profile

FACEBOOK

Let us revise case class declaration.

Java Gladiator 526 likes

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.

Like Page Be the first of your friends to like this

LABELS

Design Patterns Interview Java Java Interview Programs

Let us create 2 auxiliary constructor in below code -

Java Scenario Based Interview Scala

case class Person(id:Int, name: String)

Spring

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) Posted by Prithvipal Singh at 23:17 Labels: Scala

No comments: Post a Comment

Newer Post

18

Home

Older Post

Subscribe to: Post Comments (Atom)

Picture Window theme. Powered by Blogger.

Share

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.