Showing posts with label alaad| Companion Object. Show all posts
Showing posts with label alaad| Companion Object. Show all posts

Tuesday, September 13, 2016

Companion Class & Companion Object : Example...

A Companion object is an object that carries the Same name of the class it is supporting. In this example we have used Companion object to implement a Factory Object to instantiate the class Subject without the need to use new keyword #107
scala> :paste
// Entering paste mode (ctrl-D to finish)

class Subjects{
   private var subjectList:List[String] = Nil
   def addSubject(subject:String):Unit = {
     subjectList = subject :: subjectList
   }
   def getSubjects():List[String] = subjectList
}

object Subjects{
   def apply():Subjects = new Subjects()
}

// Exiting paste mode, now interpreting.

defined class Subjects
defined module Subjects

scala> // No need to use 'new' here...

scala> val subjects = Subjects()
subjects: Subjects = Subjects@385e9564

scala> subjects.addSubject("math")

scala> subjects.addSubject("Science")

scala> subjects.getSubjects()
res2: List[String] = List(Science, math)

Monday, September 12, 2016

Array Example : Value & Type Paramaterization

scala> // Parameterize Class instance with Value & Type

scala>

scala> /*******************************************
     |    Creating & Initializing an Array #77
     | *******************************************/
     | //Option 1
     | // Here 'String'  is Parameterized Type
     | //      '2'       is a Parameterized Value
     | // Square Bracket ([]) is used to Parameterize a Type
     | val subject:Array[String] = new Array[String](2)
subject: Array[String] = Array(null, null)

scala> subject.update(0, "english")

scala> subject.update(1, "math")

scala>

scala> //Option 2

scala> val subject = new Array[String](3)
subject: Array[String] = Array(null, null, null)

scala> // During assignment Parenthesis calls the 'update()' method

scala> // passing both the Array index & value assigned

scala> subject(0) = "english"

scala> subject(1) = "math"

scala>

scala> // Option 3

scala> // Here Parenthesis in 'Array' calls the apply() factory

scala> // method of the Companion Object (Here Array is a

scala> // Companion Object rather than the Class)

scala> val subject = Array("english", "math")
subject: Array[String] = Array(english, math)


scala> /*******************************************
     |    Accessing an Array #77
     | *******************************************/
     |
     | //Option 1
     | for(i <- (0).to(1)) {
     |    // Note : Parenthesis is used to access an Array element
     |    //        unlike Square brackets that is used in Java
     |    //        #79
     |    print(subject.apply(i))
     | }
englishmath
scala>

scala> //Option 2

scala> // If a method ('to' in our case) takes only 1 parameter

scala> // '.' and Parenthesis cna be omitted (We should also have a

scala> // receiver for the method call...in our case 'i')

scala> for(i <- 0 to 1) {
     |    //Note here..the apply() method is called implicitly
     |    print(subject(i))
     | }
englishmath