Scala – Constructors/Modifiers

  • date 8th October, 2020 |
  • by Prwatech |
  • 0 Comments

Constructor:

1. Primary Constructor – compiler creates a constructor which is known as primary constructor. All the statements of class body treated as part of constructor. It is also known as default constructor.

      Run the following program to execute Primary Constructor:

Output:

2. Scala Secondary (auxiliary) Constructor – Let’s call primary constructor from inside the auxiliary constructor. This keyword is used to call constructor from other constructor. When calling other constructor make it first line in your constructor.

Run the following command to execute secondary (auxiliary) constructor:

OUTPUT:

         

Modifiers:

1.     No Modifiers

2.     Private

3.     Protected

Accessibility chart:

 Outside PackageSame PackageSame ClassSub ClassCompanion Class
No ModifierYesYesYesYesYes
PrivateNoNoYesNoYes
ProtectedNoNoYesYesYes

 

·  Singleton Object

·  Companion Class

Example program of Singleton Object

Quick Support

image image