Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, every class has at least one constructor, which is the primary constructor. This primary constructor is not defined with a method named "constructor" or any similar special keyword. Instead, it's interwoven with the class definition itself.
Parameters: The parameters of the primary constructor are placed immediately after the class name.
Body Execution: The entire body of the class acts as the primary constructor's body. This means that any code present in the class body will get executed when an object of the class is instantiated.
Auxiliary Constructors: A class can also have one or more secondary (or auxiliary) constructors. These are named this
and must ultimately call the primary constructor (either directly or indirectly).
Here's an example to showcase primary constructors in Scala:
class Person(name: String, age: Int) { println(s"Creating a new person named $name who is $age years old.") // Auxiliary constructor def this(name: String) { this(name, 0) // calling the primary constructor println(s"Age not provided for $name, defaulting to 0.") } def greet(): Unit = { println(s"Hello! My name is $name and I'm $age years old.") } } val p1 = new Person("Alice", 25) // Outputs: Creating a new person named Alice who is 25 years old. p1.greet() // Outputs: Hello! My name is Alice and I'm 25 years old. val p2 = new Person("Bob") // Outputs: Creating a new person named Bob who is 0 years old. // Age not provided for Bob, defaulting to 0. p2.greet() // Outputs: Hello! My name is Bob and I'm 0 years old.
Private Constructor: You can make the primary constructor private by adding the private
modifier before the constructor parameters:
class Singleton private() { //... }
This is often used in the Singleton design pattern to ensure that no other instances can be created from outside the class.
Visibility Modifiers: By default, the parameters of the primary constructor become fields of the class but are not accessible outside of it. If you want to make them publicly accessible, you can add val
or var
:
class Person(val name: String, var age: Int)
With this definition, name
is a read-only field, while age
is mutable.
Scala's primary constructor concept is a bit different from many other programming languages, as it seamlessly integrates with the class definition. This results in concise class and constructor definitions, one of Scala's hallmark features.
Defining Parameters in Scala Primary Constructor:
Parameters in the primary constructor are defined directly within the class definition.
class Person(name: String, age: Int)
Initializing Class Members in Scala Primary Constructor:
Parameters in the primary constructor can be directly used to initialize class members.
class Person(name: String, age: Int) { println(s"Creating a person named $name with age $age") }
Access Modifiers in Scala Primary Constructor:
Access modifiers can be applied to constructor parameters to control visibility.
class Person(private val name: String, val age: Int) { def getName: String = name }
Default Values in Scala Primary Constructor Parameters:
Default values can be provided for constructor parameters.
class Person(name: String = "John", age: Int = 30)
Secondary Constructors vs Primary Constructor in Scala:
Secondary constructors are additional constructors defined using def this(...)
, while the primary constructor is part of the class header.
class Person(name: String, age: Int) { // Primary constructor def this() = this("John Doe", 25) // Secondary constructor }
Inheritance and Primary Constructors in Scala:
Subclasses can extend the primary constructor of their superclass.
class Student(name: String, age: Int, studentId: String) extends Person(name, age)
Case Class Primary Constructor in Scala:
Case classes automatically generate a primary constructor based on the class parameters.
case class Point(x: Int, y: Int)
Visibility Modifiers in Scala Primary Constructor:
Visibility modifiers can be applied to constructor parameters for encapsulation.
class Person(private val name: String, protected val age: Int)