How to create a class in Scala

In this shot, we will learn how to create various classes in Scala, an object-oriented language.

Characteristics of a class

A class defines the structure of a program. It is basically the blueprint of your program, and it includes characteristics such as:

  • Constructors
  • Members
  • Fields
  • Properties
  • Methods
  • Instances or objects, etc.

Note: In Scala, members are public by default. The keyword private needs to be added before declaring a variable to limit its access within the class.

Types of classes in scala

We can define multiple types of classes in Scala, such as:

  • Generic classes
  • Case classes
  • Normal classes

Every class has one thing in common: the keyword class, which is used to define it.

Simple classes

In Scala, when we create a simple class, we define the class keyword followed by the name of the class. This structure is similar to how we create a class in C++ or Java.

Code

Let’s look at the implementation of a simple class named Vehicle through the following example:

class Vehicle {
var carName: String = "Wagon R"
var companyName: String = "Suzuki"
var carPlateNo: Int = 69
def CarDetails() : Unit = {
println("Name of the Car: " + carName);
println("Name of Company: " + companyName);
println("Car Number plate no: " + carPlateNo);
}
}
object Main {
def main(args: Array[String]) : Unit = {
var obj = new Vehicle();
obj.CarDetails();
}
}

Explanation

The above code snippet implements a simple class, demonstrating how we can declare a function CarDetails and print the details of Vehicle that are stored in its variables.

Generic Classes

Generic classes are classes that take a type – just like a parameter – inside the square brackets [].

This type of class is helpful for collection classes.

Naming convensions

Generic classes in Scala have particular naming conventions when implementing a class.

  • Letter A is written inside ([]) as a type parameter
  • Symbol used to denote a key is A, and for a value, it is B
  • Symbol used to denote a numeric value is N

Code

Let’s look at the implementation of generic classes through the following example:

object GenericClass {
def main(args: Array[String]) : Unit = {
abstract class Addition[A] {
def addition(b: A, c: A): A
}
class intAddition extends Addition[Int] {
def addition(b: Int, c: Int): Int = b + c
}
class doubleAddition extends Addition[Double] {
def addition(b : Double, c : Double) : Double = b + c
}
val add1 = new intAddition().addition(69, 5)
val add2 = new doubleAddition().addition(30.0, 8.0)
println(add1)
println(add2)
}
}

Explanation

The above code snippet implements a generic class, demonstrating how we can use the same function (addition()) in multiple classes to add distinct data types and display their respective results.

Case Classes

Case classes in Scala are similar to regular classes, but they include a few new keywords.

Case classes are suitable for modeling immutablean object whose state cannot be modified after its creation data.

Note: All parameters listed in a case class are public and immutable by default. In a normal class, parameters are private by default.

Code

The following is an example of adding two variables inside of a case class:

case class CaseClass (a:Int, b:Int)
object MainObject {
def main(args:Array[String]) : Unit = {
var c = CaseClass(10,10)
println("Sum of a & b = " + c.a + c.b)
//println("b = " + c.b)
}
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved