Calling A Super Class Constructor in Scala

Prerequisite – Scala Constructors

In Scala, Constructors are used to initialize an object’s state and are executed at the time of object creation. There is a single primary constructor and all the other constructors must ultimately chain into it. When we define a subclass in Scala, we control the superclass constructor that is called by its primary constructor when we define the extends portion of the subclass declaration.

  • With one constructor: An example of calling a super class constructor
    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to illustrate
    // calling a super class constructor
      
    // Primary constructor
    class GFG (var message: String)
    {
        println(message)
    }
      
      
    // Calling the super class constructor
    class Subclass (message: String) extends GFG (message) 
    {
        def display()
        {
            println("Subclass constructor called")
        }
    }
      
    // Creating object
    object Main 
        // Main method
        def main(args: Array[String]) 
        
              
            // Creating object of Subclass 
            var obj = new Subclass("Geeksforgeeks"); 
            obj.display();
        

    chevron_right

    
    

    Output:

    Geeksforgeeks
    Subclass constructor called
    

    In the above example, the subclass is defined to call the primary constructor of the GFG class, which is a single argument constructor that takes message as its parameter. When defining a subclass in Scala, one controls the Superclass constructor that’s called by the Subclass’s primary constructor when defining the extends segment of the Subclass declaration.

  • With multiple constructors : In a case with the Superclass having multiple constructors, any of those constructors can be called using the primary constructor of the Subclass. For Example, in the following code, the double argument constructor of the Superclass is called by the primary constructor of the Subclass using the extends clause by defining the specific constructor.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to illustrate
    // calling a specific super class constructor
      
    // Primary constructor (1)
    class GFG (var message: String, var num: Int)
    {
          
        println(message+num)
          
        // Auxiliary constructor (2)
        def this (message: String) 
        {
            this(message, 0)
              
        }
          
    }
      
    // Calling the super class constructor with 2 arguments
    class Subclass (message: String) extends GFG (message, 3000)
    {
        def display()
        {
            println("Subclass constructor called")
        }
    }
      
    // Creating object
    object GFG 
        // Main method
        def main(args: Array[String]) 
        
              
            // Creating object of Subclass 
            var obj = new Subclass("Article count "); 
            obj.display();
        

    chevron_right

    
    

    Output:

    Article count 3000
    Subclass constructor called
    
  • We can call the single argument constructor here, By default another argument value will be 0.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program to illustrate
    // calling a specific super class constructor
      
    // Primary constructor (1)
    class GFG (var message: String, var num: Int) 
    {
          
        println(message + num)
          
        // Auxiliary constructor (2)
        def this (message: String) 
        {
            this(message, 0)
              
        }
          
    }
      
      
    // Calling the superclass constructor with 2 arguments
    class Subclass (message: String) extends GFG (message) 
    {
        def display()
        {
            println("Subclass constructor called")
        }
    }
      
    // Creating object
    object GFG 
        // Main method
        def main(args: Array[String]) 
        
              
            // Creating object of Subclass 
            var obj = new Subclass("Article Count "); 
            obj.display();
        

    chevron_right

    
    

    Output:

    Article Count 0
    Subclass constructor called
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.