Scala Lesson 30 – Companion Objects | Dataplexa

Companion Objects

In this lesson, you will learn about Companion Objects in Scala. Companion objects are a powerful feature that allows you to group related logic with a class.

They are commonly used to define factory methods, shared utilities, and constants associated with a class.


What Is a Companion Object?

A companion object is an object that has the same name as a class and is defined in the same source file.

The class and its companion object can access each other's private members. This makes them tightly coupled.


Basic Companion Object Example

Below is a simple example of a class and its companion object.

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

object Person {
  def greet(): String = "Welcome to Scala!"
}

Here:

  • Person is a class
  • Person object is its companion object
  • Both share the same name and file

Accessing Companion Object Methods

Methods inside a companion object are accessed using the class name.

println(Person.greet())

You do not need to create an instance to access companion object methods.


Using Companion Objects as Factory Methods

One of the most common uses of companion objects is to create factory methods.

class User(val username: String, val role: String)

object User {
  def createAdmin(name: String): User =
    new User(name, "Admin")

  def createGuest(name: String): User =
    new User(name, "Guest")
}

Now you can create objects without exposing constructor details.


Using apply() Method

If a companion object defines an apply() method, you can create instances without using new.

class Product(val name: String, val price: Double)

object Product {
  def apply(name: String, price: Double): Product =
    new Product(name, price)
}

Usage:

val p = Product("Laptop", 1200.0)

This syntax looks clean and natural.


Accessing Private Members

A class and its companion object can access each other's private members.

class Account(private val balance: Double)

object Account {
  def showBalance(acc: Account): Double =
    acc.balance
}

This is not possible between unrelated classes.


Why Companion Objects Are Important

  • Encapsulate related logic
  • Provide factory methods
  • Replace static methods (Scala has no static)
  • Improve code organization

📝 Practice Exercises


Exercise 1

Create a class Book with a companion object that prints book details.

Exercise 2

Create a companion object with an apply() method.

Exercise 3

Use a companion object to access a private field of a class.


✅ Practice Answers


Answer 1

class Book(val title: String)

object Book {
  def info(book: Book): String =
    s"Book title: ${book.title}"
}

Answer 2

class Movie(val name: String)

object Movie {
  def apply(name: String): Movie =
    new Movie(name)
}

Answer 3

class Wallet(private val cash: Int)

object Wallet {
  def getCash(w: Wallet): Int = w.cash
}

What’s Next?

In the next lesson, you will learn about Implicit Parameters and how Scala automatically supplies values when needed.