Scala class explained in detail

 218 total views,  1 views today

Welcome to Scala series. In this tutorial, you will learn about Scala class explained in detail

Scala class explained in detail

A Class is a blueprint of an object. A class defines an object. In other words, we can say the class is the template of an object.

A Class can have fields, methods, constructors, extends another class or implement an interface or Trait.

Class Syntax

class StaticReference {
  var i: Int = 1;
  var j: String = "a";
}

The above is an example of a simple class having two variables.

How to create an object of the class.

We have to use the new keyword to create an object of the class.

var staticReference = new StaticReference(); //option 1
var staticRef2:StaticReference = new StaticReference(); //option 2

Constructor

Often, you will need to initialize the class with certain parameters through a constructor.

In the below, the class doesn’t have any constructor.

class StaticReference {
  var i: Int = 1;
  var j: String = "a";
  
  
}

object Test {
  var staticReference = new StaticReference();
  var staticRef2:StaticReference = new StaticReference();
}

Constructor with arguments

This is an example of a class with default constructor. By default all the arguments passed to class are constructor arguments

class StaticReference(var str: String) {
  var i: Int = 1;
  var j: String = "a";

}

object Test {

  def main(args: Array[String]): Unit = {
    var staticRef1 = new StaticReference("abc");
    println(staticRef1.str);

    var staticRef2: StaticReference = new StaticReference("xyz");
    println(staticRef2.str);
  }

}

Output

abc
xyz

Multiple argument constructors

In a few cases, we will need multiple argument constructor to create an object. In this case, we have to define the constructor using this keyword.

class StaticReference() {
  var i: Int = 1;
  var j: String = "a";

  def this(i: Int) {
    this();
    this.i = i;
  }

  def this(i: Int, j: String) {
    this();
    this.i = i;
    this.j = j;
  }

}

object Test {

  def main(args: Array[String]): Unit = {
    var staticRef = new StaticReference(); //No args constructor
    println(staticRef.i);
    println(staticRef.j);

    var staticRef1 = new StaticReference(10); //Single arg constructor
    println(staticRef1.i);
    println(staticRef1.j);

    var staticRef2: StaticReference = new StaticReference(2, "abc"); 
   //Two args constructor
    println(staticRef2.i);
    println(staticRef2.j);
  }

}

Methods

A class can 0 to N number of methods and each method has to accessed by the object of the class.

class StaticReference() {

  def add(i: Int, j: Int): Int = {
    i + j
  }

  def multiply(i: Int, j: Int): Int = {
    i * j
  }

  def divide(i: Int, j: Int): Int = {
    i / j
  }
}

object Test {

  def main(args: Array[String]): Unit = {
    var staticRef = new StaticReference();
    println(staticRef.add(2, 3))
    println(staticRef.multiply(2, 3))
    println(staticRef.divide(2, 3))
  }

}

Output

5
6
0

Private and Public methods

By default, all methods in a class are public methods. However, in a few cases, we would need to restrict the methods to be used only inside the class. In this case, we have to mark the methods as private

class StaticReference() {

  def add(i: Int, j: Int): Int = {
    multiply(i, j)
    i + j
  }

  private def multiply(i: Int, j: Int): Int = {
    i * j
  }

  private def divide(i: Int, j: Int): Int = {
    i / j
  }
}

object Test {

  def main(args: Array[String]): Unit = {
    var staticRef = new StaticReference();
    println(staticRef.add(2, 3))
    println(staticRef.multiply(2, 3)) //Compilation error
    println(staticRef.divide(2, 3)) //Compilation error
  }

}

Error: method multiply in class StaticReference cannot be accessed in StaticReference

References: https://docs.scala-lang.org/tour/classes.html

Thanks for reading and hope you like this tutorial. Please leave a comment and give us a thumbs up!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.