In Scala, both a class and an object can share the same name.
When an object shares a name with a class, it's called a companion object, and the class is called a companion class.
A companion object is an object that shares the same name and source file with another class or trait.
A trait could be seen as a Java interface.
This approach allows us to create static members on a class.
The companion object is useful for implementing helper methods and factory.
To implement a factory that creates different types of shapes, we can create a shape factory in Scala.
We use a companion class Shape and a companion object Shape, which acts as a factory.
trait Shape { def area :Double } object Shape { private class Circle(radius: Double) extends Shape{ override val area = 3.14*radius*radius } private class Rectangle (height: Double, length: Double)extends Shape{ override val area = height * length } def apply(height :Double , length :Double ) : Shape = new Rectangle(height,length) def apply(radius :Double) : Shape = new Circle(radius) } object Main extends App { val circle = Shape(2) println(circle.area) val rectangle = Shape(2,3) println(rectangle.area) }
A singleton object that does not share the same name with a companion class is called a standalone object.