Given the case class, what will be the result of calling method inside the object accessing private field of case class?
class Person(val name: String, private val superheroName: String) //The superhero name is private!

object Person {
  def showMeInnerSecret(x: Person) = x.superheroName
}

val clark = new Person("Clark Kent", "Superman")
val peter = new Person("Peter Parker", "Spider-Man")

Person.showMeInnerSecret(clark)
Person.showMeInnerSecret(peter)
Explanation
A companion object can also see private values and variables of the corresponding classes' instantiated objects.

Source: Scala Exercises: objects

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback