Scala

Scala Companion Object

By defining a class object, we can inspect the class’s members and invoke any method in other programming languages. The specific method is called with the help of that object. In the article, we’ll make an object that doesn’t need a class to declare its members. Scala introduces singleton objects or companion objects. Scala class and Scala object are with the same source file then; they are a companion to each other.

What are Companion Objects in Scala?

A companion object and the class are in the same source file along with the same name. Companion objects are objects that have a similar name to the class they belong to. The same object and class name combination are very powerful, we can access both private methods and fields of the class.

Syntax of Scala Companion Objects

The general syntax of Companion Object in Scala looks in this way:

Class class_name
{//class body}
Object object_name
{//object body}

Here, “class_name” is the name of the class and the object_name is the name of the class object. The class and object name should be the same to become companions to each other. This means that we can use the fields and methods of the instances of the class.

How to Use the Scala Companion Objects in Ubuntu 20.04?

To understand the usage of the Companion Objects in Scala, you should take a look at the following examples below:

Example 1: Using Scala Companion Class and Object Method

We can create a companion object by keeping the name of the object the same as the class name. Then, we considered the class and object companion to each other. Below, we have created a companion object.

In the above code, we have a class as Example1. The class has variables Str1 and Str2 initialized with the string value. There is a method “myExample” that defines the class which prints the “str1” and “str2” values. In the companion object, we are accessing the method of the relevant class.

You can see in the output below; we can access the member of the companion class with the “new” keyword into the companion object.

Example 2: Using Apply Method for Scala Companion Object

We can create an instance of a class through the apply method. Let’s have an apply method implementation below.

In the above code, we have declared a class named “fruits” and also created an object of the Class. The class object is also named “fruits”. By keeping their name, the same they are considered companion objects. The class fruit has been defined with a variable “x”. The variable has an empty string. Next, we have a companion object of companion class fruit. In the companion object, we have used the apply method which is taking an argument of a companion class member. Then, we have created a variable ‘y’ in the apply method. The variable “y” is initializing an instance of an object with the keyword “new”. By using the apply method, we can call the object from the class directly.

We are using the paste mode for the apply method. After exiting from a paste mode, we can call the member of the class directly. We have written a pattern matching code in Scala and the output is shown below.

Example 3: Using Apply Method for Creating Multiple Constructors

By using the apply method, we can pass more than one argument. We can have an apply method in the companion class more than one that will provide multiple constructors. The example is having two constructors in the apply method and two apply methods in the companion object.

In the above code, we have created a companion class as “language” and defined two variables “l1” and “l2”. Then, we have a companion object of the name “language” the same as the companion class name. In the companion object, we have used two apply methods. The first is having a single value passed and the other apply method takes two arguments as a constructor. The apply method has the variable “l3” which has a “new” keyword with the object. We can generate a new instance of an object with the usage of the “new” keyword. The “l3” takes values from “l1” and “l2” in the code.

The output we can access through paste mode as implementation is shown after exiting mode. We can call the object by using the apply method.

Example 4: Using Unapply Method for Companion Objects

Now, we can also extract the field from an object by using the unapply method in the companion object.

In the above code, first, we have made a companion class which is named “color”. The companion class “color” has variables as members of the class. The variable is represented as “c1” and “c2”. These variable fields can be accessed by the companion object as a constructor. Then, we have created a companion object and defined the apply method in it. The apply method creates a constructor by passing arguments of companion class fields. In the apply method, we have a variable “c3” which is using a new keyword with the object color. The values of “c1” and “c2” have passed to “c3”. In the end, we have to use the unapply method that has “c3” as an argument and returns the value of “c1” and “c2”.

After exiting from paste mode, we have extracted the fields from a “c3”. The output of the unapply method is shown on the screen.

Conclusion

The guide aims to demonstrate to you the benefits of having a companion object. To become a companion to the class and object, the name should be the same and are kept in the same source file. Then, we discussed apply method with the code implementation. We can use more than one apply method in a companion object and create various constructors in the apply method. We have also discussed the unapply method to extract the fields of the class. The apply method example is through the paste mode in Ubuntu 20.04 terminal. You can use paste mode by giving the “scala” command on the terminal and also exiting from paste mode by pressing “Ctrl+D”.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.