Scala – Primary Constructor

  • date 23rd April, 2021 |
  • by Prwatech |
  • 0 Comments

Scala program which contains only one constructor then that constructor is known as Primary constructor.

syntax:-

class class name(parameter list) {

//statements

}

use case 1:

scala> class smartphone(val size:Int, val price:Double){

     println(“Inside smartphone constructor”)

     def desc = “smartphone price” + price + “is of size” + size

     }

use case 2:

scala> class Dog(val size:Int, val height:Double){

     println(“Inside dog constructor”)

     def desc = “Dog height” + height + “is of size” + size

     }

use case 3:

scala> class Fruit(val name:String,val color:String, val weight:Double){

      println(“Inside dog constructor”)

      def desc = “Fruit name” + name + “is of color” + color + “and weight is” + weight

      }

Quick Support

image image