Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Calling a Super Class Constructor in Python
Next article icon

Calling A Super Class Constructor in Scala

Last Updated : 28 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 
     

Scala




// 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();
    }
}
 
 
  •  
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: 
     

Scala




// 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();
    }
}
 
 
  •  
Output: 
Article count 3000 Subclass constructor called

 

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

Scala




// 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 1 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();
    }
}
 
 
  •  
Output: 
Article Count 0 Subclass constructor called

 

 



Next Article
Calling a Super Class Constructor in Python

V

vishodushaozae
Improve
Article Tags :
  • Scala
  • Scala
  • Scala-Constructor

Similar Reads

  • Calling a Super Class Constructor in Python
    Classes are like creating a blueprint for an object. If we want to build a building then we must have the blueprint for that, like how many rooms will be there, its dimensions and many more, so here the actual building is an object and blueprint of the building is a class. A Class is a user-defined
    4 min read
  • Call a method on a Super Class in Scala
    This concept is used when we want to call super class method. So whenever a base and subclass have same named methods then to resolve ambiguity we use super keyword to call base class method. The keyword "super" came into this with the concept of Inheritance. Below is the example of call a method on
    2 min read
  • Constructor in Java Abstract Class
    Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor. So if we do not define any constructor inside the abstract class then JVM (Java Virtual Machine) will g
    4 min read
  • How to call the constructor of a parent class in JavaScript ?
    In this article, we learn how to call the constructor of a parent class. Before the beginning of this article, we should have a basic knowledge of javascript and some basic concepts of inheritance in javascript. Constructor: Constructors create instances of a class, which are commonly referred to as
    4 min read
  • Scala | Auxiliary Constructor
    Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements (i.e. instructions). Statements are executed at the time of object creation. In Scala Program, the constructors other than the primary constructor are known as Auxiliary Con
    3 min read
  • How to call base class constructor from child class in TypeScript ?
    In this article, we will learn how we can call the base class constructor from the child class. When a class inherits the properties of another class, it is called a child class and the class whose properties are inherited is called the parent class and the whole process is called Inheritance. In In
    3 min read
  • java.lang.reflect.Constructor Class in Java
    java.lang.reflect.Constructor class is used to manage the constructor metadata like the name of the constructors, parameter types of constructors, and access modifiers of the constructors. We can inspect the constructors of classes and instantiate objects at runtime. The Constructor[] array will hav
    4 min read
  • Scala | Primary Constructor
    Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions). statements are executed at the time of object creation. When our Scala program contains only one constructor than that constructor is called a primary co
    4 min read
  • Scala Constructors
    Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. Scala supports two types of constructors: Primary Constructor When our Scala program contains only one c
    4 min read
  • Generic Classes in Scala
    In Scala, forming a Generic Class is extremely analogous to the forming of generic classes in Java. The classes that takes a type just like a parameter are known to be Generic Classes in Scala. This classes takes a type like a parameter inside the square brackets i.e, [ ]. This classes are utilized
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences