In scala , companion object is an object with same name as class name.

example:
class employee {
}
object employee{
}

companion object and class will share its private members .

example
class employee {
def display = { println (“this is demo of access private”+securityname)}
}
object employee{
private val securityname = “secret”
}

apply method makes to create object with out newkey word , should be defined in object. apply is basically a constructor like thing , we can have more than one apply method to have multiple constructors concept.

example
class employee{
var name:option[String] =None
var age:option[Int]=None
override def toString=…

kumar ramanathan

Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store