1.

What Is The Use Of Auxiliary Constructors In Scala?please Explain The Rules To Follow In Defining Auxiliary Constructors In Scala?

Answer»

In Scala, The main purpose of Auxiliary Constructors is to overload constructors. LIKE Java, We can provide various kinds of constructors so that USE can choose the right one BASED on his requirement.

Auxiliary Constructor Rules:

  • They are like methods only. Like methods, we should use ‘def’ keyword to define them.
  • We should use same NAME ‘this’ for all Auxiliary Constructors.
  • Each Auxiliary Constructor should start with a call to previous defined another Auxiliary Constructor or Primary Constructor. Otherwise compile-time error.
  • Each Auxiliary Constructor should differ with their parameters list: may be by number or types.
  • Auxiliary Constructors cannot call a super class constructors. They should call them through Primary Constructor only.
  • All Auxiliary Constructors call their Primary Constructor either directly or indirectly through other Auxiliary Constructors.

NOTE:- If you want to learn about Scala’s Constructors, please refer my Scala posts at: Primary Constructor and Auxiliary Constructor.

In Scala, The main purpose of Auxiliary Constructors is to overload constructors. Like Java, We can provide various kinds of constructors so that use can choose the right one based on his requirement.

Auxiliary Constructor Rules:

NOTE:- If you want to learn about Scala’s Constructors, please refer my Scala posts at: Primary Constructor and Auxiliary Constructor.



Discussion

No Comment Found