Scala - Companion Objects



This chapter takes you through the concept of companion objects in Scala programming. Companion objects are objects that share the same name as a class and are defined in the same source file. These can access the private fields and methods of their companion class. So, provide a way to associate static-like functionality with the class.

Companion Objects

Companion object is a singleton object. It has the same name as the class name and is defined in the same source file. Companion objects can access the private fields and methods of their companion class.

Syntax

The syntax of the companion objects are -

class ClassName {
  // Class fields and methods
}

object ClassName {
  // Companion object fields and methods
}

Example

The following example shows a simple companion object in Scala programming.

class Circle(val radius: Double) {
  def area: Double = Circle.Pi * radius * radius
}

object Circle {
  val Pi: Double = 3.14159
  
  def apply(radius: Double): Circle = new Circle(radius)
}

object Demo {
  def main(args: Array[String]) = {
    val circle = Circle(5.0)
    println(s"Circle radius: ${circle.radius}")
    println(s"Circle area: ${circle.area}")
  }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Circle radius: 5.0
Circle area: 78.53975

In the example, the Circle class has a method area. This method calculates the area of the circle using the constant Pi defined in its companion object. The companion object Circle also defines an apply method. This is factory method to create instances of the Circle class. The Demo object has the main method.

Accessing Private Fields Using Companion Objects

Companion objects can access the private fields and methods of their companion class. So you can close interaction between the class and its companion object.

Example

The following example shows how a companion object can access private fields of its companion class.

class BankAccount(private var balance: Double) {
  def deposit(amount: Double): Unit = {
    if (amount > 0) balance += amount
  }

  def currentBalance: Double = balance
}

object BankAccount {
  def apply(initialBalance: Double): BankAccount = new BankAccount(initialBalance)
  
  def reset(account: BankAccount): Unit = {
    account.balance = 0  // Accessing private field of the companion class
  }
}

object Demo {
  def main(args: Array[String]) = {
    val account = BankAccount(1000.0)
    println(s"Initial balance: ${account.currentBalance}")
    account.deposit(500.0)
    println(s"Balance after deposit: ${account.currentBalance}")
    BankAccount.reset(account)
    println(s"Balance after reset: ${account.currentBalance}")
  }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Initial balance: 1000.0
Balance after deposit: 1500.0
Balance after reset: 0.0

In the example, the BankAccount class has a private field balance and methods to modify it. The companion object BankAccount defines a reset method. This method can access and modify the private field balance of the BankAccount class. The Demo object uses the BankAccount class and its companion object.

Companion Objects for Utility Methods

Companion objects are used to define utility methods. These methods are logically related to the class but do not require an instance of the class to be accessed.

Example

class Temperature(val celsius: Double) {
  def toFahrenheit: Double = Temperature.celsiusToFahrenheit(celsius)
}

object Temperature {
  def celsiusToFahrenheit(celsius: Double): Double = celsius * 9 / 5 + 32

  def fahrenheitToCelsius(fahrenheit: Double): Double = (fahrenheit - 32) * 5 / 9
}

object Demo {
  def main(args: Array[String]) = {
    val temp = new Temperature(25)
    println(s"Temperature in Celsius: ${temp.celsius}")
    println(s"Temperature in Fahrenheit: ${temp.toFahrenheit}")

    println(s"50 Fahrenheit in Celsius: ${Temperature.fahrenheitToCelsius(50)}")
  }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Temperature in Celsius: 25.0
Temperature in Fahrenheit: 77.0
50 Fahrenheit in Celsius: 10.0

In the example, the Temperature class represents a temperature in Celsius. It also has a method toFahrenheit that converts the temperature to Fahrenheit using a method defined in its companion object. The companion object Temperature defines utility methods for temperature conversion. The Demo object uses the Temperature class and its companion object.

Companion Objects for Factory Methods

Companion objects can define factory methods that create instances of the class. This approach provides more flexibility and can encapsulate creation logic.

Example

class Person(val name: String, val age: Int)

// Define the Person class
class Person(val name: String, val age: Int)

// Companion object for Person
object Person {
  def apply(name: String, age: Int): Person = new Person(name, age)
  def apply(name: String): Person = new Person(name, 0)  // Default age to 0
}

object Demo {
  def main(args: Array[String]) = {
    val person1 = Person("Alice", 30)
    val person2 = Person("Bob")
    println(s"Person1: ${person1.name}, Age: ${person1.age}")
    println(s"Person2: ${person2.name}, Age: ${person2.age}")
  }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Person1: Alice, Age: 30 
Person2: Bob, Age: 0

In the example,The Person class has two properties: name and age. It also has a companion object Person with apply methods for creating Person instances. The Demo object has the main method for creating Person objects and printing their details.

Scala Companion Objects Summary

  • Companion objects are singleton objects. These have the same name as a class and are defined in the same source file.
  • Companion objects can access the private fields and methods of their companion class.
  • Companion objects are used in defining factory methods, utility functions, and constants related to the class.
  • Companion objects provide a way to associate static-like functionality with a class.
Advertisements