The injector (or) default method : apply()

The apply method in a class can be called using the Round brackets...ie (). This helps to create a convention wherein a Method in an Object is called like a Function
scala> class SumOf {
     |    def apply(no1: Int, no2: Int) : Int = {
     |       no1 + no2
     |    }
     | }
defined class SumOf

scala> val sum = new SumOf()
sum: SumOf = SumOf@1b54903a

scala> // Calling the method apply() explicitly

scala> println(sum.apply(3, 2))
5

scala> // apply() method called without its name using

scala> // Round brackets...ie ()

scala> println(sum(3, 2))
5

No comments:

Post a Comment