Home »
Scala
Calling a Constructor of Super Class in Scala
By IncludeHelp Last updated : October 26, 2024
Scala Super Class Constructors
A Constructor a method of the class that is used to initialize the object state in Scala class and are invoked by the compiler when the object of the class is created. In Scala programming language you can create any number of constructors for a class but there needs to be a single primary constructor which is the end of the constructor chain.
Calling Super Class Constructors
If we define a subclass, then it has to by default call any of the constructors of the superclass in its object creation. The calling constructor of the superclass can be a call to any of the available constructor of the superclass (primary or auxiliary).
The following are the different ways to call super class constructor:
- Without constructor (only primary constructor)
- With Multiple constructor, calling primary constructor first
- With Multiple constructor, calling primary constructor first
Without Constructor (Only Primary Constructor)
When utilizing multiple constructors, the primary constructor is invoked first to ensure proper initialization of the superclass.
This example demonstrates how to call a superclass constructor in Scala when only using the primary constructor:
class Bike(var message: String) {
def printMessage(): Unit = {
println("I have " + message)
}
}
class Speeding(message: String) extends Bike(message) {
def display(): Unit = {
printMessage()
println("Bike goes Brooom!")
}
}
object MyObject {
def main(args: Array[String]): Unit = {
val newbike = new Speeding("Ninja H2R")
newbike.display()
}
}
Output
I have Ninja H2R
Bike bike goes Brooom!
Explanation
The code shows how to call the constructor of the superclass? Here there is only one constructor in the superclass.
The superclass named "bike" has a constructor that prints a line. The subclass is named "speeding" has a method named display() which prints something to string.
With Multiple Constructors, Calling Primary Constructor First
When using multiple constructors, the primary constructor is called first, allowing for proper initialization of the superclass. The following example demonstrates this concept:
class Bike(var bikename: String, var speed: Int) {
// Constructor prints message when a new instance is created
println(s"I have $bikename. It goes to a speed of $speed")
}
class Speeding(bikename: String) extends Bike(bikename, 412) {
def display(): Unit = {
println("Bike goes Brooom!")
}
}
object MyObject {
def main(args: Array[String]): Unit = {
val newbike = new Speeding("Ninja H2R")
newbike.display()
}
}
Output
I have Ninja H2R. It goes to a speed of 412
Bike bike goes Brooom!
Explanation
The code shows how to call the constructor of the superclass? Here there is only one constructor in the superclass.
The superclass named "bike" has a constructor that prints a line. The superclass bike has two constructors, the primary constructor takes two arguments and there is an auxiliary constructor that handles single argument. The subclass is named "speeding" has a method named display() which prints something to string.
With Multiple Constructors, Calling Auxiliary Constructor First
When using multiple constructors, the primary constructor is called first, ensuring the superclass is properly initialized. The following example illustrates this behavior with multiple constructors:
class Bike(var bikeName: String, var speed: Int) {
println(s"I have $bikeName. It goes to a speed of $speed.")
// Auxiliary constructor with a default speed value
def this(bikeName: String) = this(bikeName, 256)
}
class Speeding(bikeName: String) extends Bike(bikeName) {
def display(): Unit = {
println("Bike goes Brooom!")
}
}
object MyApp {
def main(args: Array[String]): Unit = {
val newBike = new Speeding("Ninja H2R")
newBike.display()
}
}
Output
I have Ninja H2R. It goes to a speed of 256
Bike bike goes Brooom!
Explanation
The program is used to call the constructor of the superclass "bike" that has two constructors. One is primary and the other is auxiliary. The subclass "speeding" calls the auxiliary constructor which call the primary constructor as a chain.