×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Classes and Objects in Scala

By IncludeHelp Last updated : October 22, 2024

Scala Class

A class is a blueprint for objects. It contains the definition of all the members of the class. There are two types of members of the class in Scala,

  1. Fields: the variables in Scala that are used to define data in the class.
  2. Methods: The functions of a class that are used to manipulate the fields of the class and do some stuff related to the functioning of a class.

Usage of a class member to the outer worlds is limited and can be excessed by only two ways,

  1. Inheritance: Property by which members of one class are used by child class which inherits it.
  2. Objects: It is creating instances of a class to use its members.

Scala Object

Scala object is a singleton that means an object is also a class with exactly one instance, and it is used to define static methods or values and is often used for utility functions or as an entry point for an application.

Example of Scala Class and Object

We will use the classical student class for the explanation of class creation in Scala. Here, we have a class student, with fields: roll no, name, and percentage. There are methods to this class: getpercentage(), printresult().

Code

class Student(var rollno: Int, var name: String) {
  // Class Variable
  var percentage: Int = 0 // Default value set to 0

  // Class Method to set the percentage
  def getPercentage(percent: Int): Unit = {
    percentage = percent
  }

  // Class Method to print the result
  def printResult(): Unit = {
    println(s"Roll number : $rollno")
    println(s"Name  : $name")
    println(s"Has scored $percentage% and is ")
    if (percentage > 40)
      println("passed")
    else 
      println("failed")
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    // Creating an instance of Student class
    val student1 = new Student(101, "Tom Cruise")
    
    // Setting percentage using getPercentage method
    student1.getPercentage(85)
    
    // Printing the result using printResult method
    student1.printResult()

    // Creating another instance
    val student2 = new Student(102, "Alvin")
    
    // Setting a lower percentage
    student2.getPercentage(35)
    
    // Printing the result for the second student
    student2.printResult()
  }
}

Here is the output –

Roll number : 101
Name  : Tom Cruise
Has scored 85% and is 
passed
Roll number : 102
Name  : Alvin
Has scored 35% and is 
failed

Class Creation – Explained

First, the class is declared keyword class is used to create a class followed by the name of the class. Next is the definition of the class members, the class contains three members all are public (because of var declaration). It also contains two member functions(methods) that are declared using def keyword (no need of return type), followed by the name of the function and then within the "(" are the arguments that are passed when the function is called.

classes in scala

Primary Constructor in Scala

There is a new declaration of class in Scala, and you will see it very it is more efficient than the classical one.

It is the use of primary constructor definition in Scala.

Syntax

class student (var rlno , var stname){
    var rollno = rlno;
    var name = stname; 
}

Explanation

Here the class body is acting as a constructor and is used to initialize the values of fields.

This is all about classes in Scala we will learn about objects, their creation, and usage in the next tutorial.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.