October 4, 2025

Scala – Classes and Objects

In this lesson, we would be learning about Object Oriented Programming(OOP) concepts in Scala.

A Class is a template or blueprint for creating objects. Once you have defined a class, you can then create objects from the class using the new keyword.

An Object is an instance of a class. Multiple objects can be created from a class.

  1. Defining a Class
  2. Extending a Class
  3. Implicit Classes
  4. Singleton Objects

 

1. Defining a Class

Defining a class in Scala is similar to defining a class in Java. The code below defines a class called Square. The class has two variables: l and w. It also has a method called reSize() which takes two parameters.

By default, the class has an implicit constructor with is the name of the class. This constructor takes two arguments: length and width which are visible to the whole body of the class.

class Square(length: Int, width: Int) {
  var l: Int = length;
  var w: Int = width;

  def reSize(l1: Int, w1: Int): Unit = {
    l = l + l1;
    w = w + w1;
    println("New length: " + l);
    println("New width: " + w);
  }
}

 

Having defined this class, you can then use it in main program. I call the program, SquareDemo and it is of type, object as show below:

object SquareDemo {

  def main(args: Array[String]) {
    val sq = new Square(43, 90)
    sq.reSize(5,5);

  }
}

 

2. Extending a Class

Just like in Java, you can extend a class in Scala as well. To achieve this, you need to use the extends keyword in the new class.

In the program below we have a parent class Shape which has two variables x and y as well as a method move(). Then we have a child class Square that inherits from shape.

The Square class has two variables l and w as well as a reSize() method.

//Parent Class (SuperClass)
class Shape(val posX: Int, val posY: Int) {
  var x: Int = posX;
  var y: Int = posY;

  def move(dx: Int, dy: Int) {
    x = x + dx
    y = y + dy
    println ("New x position : " + x);
    println ("New y position : " + y);
  }
}

//Child Class (Subclass or Inherited Class)
class Square(length: Int, width: Int) extends Shape(5, 6){
  var l: Int = length;
  var w: Int = width;

  def reSize(l1: Int, w1: Int): Unit = {
    l = l + l1;
    w = w + w1;
    println("New length: " + l);
    println("New width: " + w);
  }
}

 

The keyword extends used in the subclass:

  • makes the Square class inherit all the members of the Shape class that are non-private. So the Square class also has members: x, y and move().
  • makes the Square class a type of Shape

 

3. Implicit Classes

Implicit class is a new feature introduced with Scala 2.10. It is achieved using the implicit keyword when creating the class. This keyword makes the class’ primary constructor available for implicit conversion if the class is in scope.

Example

object Helpers {
  implicit class IntWithTimes(x: Int) {
    def times[A](f: => A): Unit = {
      def loop(current: Int): Unit =
        if(current > 0) {
          f
          loop(current - 1)
        }
      loop(x)
    }
  }
}

In this example, we create an implicit class IntWithTimes. This class then wraps an Int value and give a  new method times().

To use this class, we simply need to import it into the scope and call it’s times method as shown below:

object Demo {
   def main(args: Array[String]) {
      6 times println("Welcome!")
   }
}

This would print  the text “Welcome” 6 times to the output.

Restrictions on Implicit Classes

  • they must be defined inside another class, object or trait
  • they can only take one non-implicit argument in their constructor
  • they cannot be any method, member or object in scope with the same name as the implicit class

 

4. Singleton Objects in Scala

The Singleton Pattern is a software engineering pattern that specifies that a class should only have on single instance.

Therefore in Scala, a singleton class is a class that allows instantiation of just one object. So you cannot have static members. And since you cannot instantiate a singleton object, you cannot also pass parameters to the primary constructor.

The examples we’ve considered previously are all singleton classes.

Leave a Reply