Home » Scala language

Calling a Constructor of Super Class in Scala

Scala Superclass constructors are called by default from the base class. In this tutorial on calling a superclass constructor in Scala, we will learn about the superclass constructor calling in Scala with examples.
Submitted by Shivang Yadav, on September 27, 2019

Scala Superclass 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.

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).

There are multiple ways for calling a constructor of superclass,

1) Without constructor (only primary constructor)

class bike (var message: String) 
{ 
    println("I have " + message) 
} 

class speeding (message: String) extends bike (message) 
{ 
    def display() 
    { 
        println("Bike bike goes Brooom! ") 
    } 
}

object MyObject 
{ 
    def main(args: Array[String]) 
    { 
        var 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.

2) With Multiple constructor, calling primary constructor first

class bike (var bikename: String, var speed : Int) 
{ 
    println("I have "+bikename+". It goes to a speed of "+speed) 
    def this(bikename : String){
        this(bikename, 156)
    }
}

class speeding (bikename: String) extends bike (bikename, 412) 
{ 
    def display() 
    { 
        println("Bike bike goes Brooom! ") 
    } 
}

object MyObject 
{ 
    def main(args: Array[String]) 
    { 
        var 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.

3) With Multiple constructor, calling primary constructor first

class bike (var bikename: String, var speed : Int) 
{ 
    println("I have "+bikename+". It goes to a speed of "+speed) 
        def this (bikename:String) {
            this(bikename, 256)
        }
} 

class speeding (bikename: String) extends bike (bikename) 
{ 
    def display() 
    { 
        println("Bike bike goes Brooom! ") 
    } 
} 

object MyObject 
{ 
    def main(args: Array[String]) 
    { 
        var 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.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.