I am new to Scala world and bit confused with OOPS concept . Here is my code snippet.
abstract class Element {
def contents: Array[String]
val lenth = contents.size
val maxLength = contents.map(_.size).max
}
class ArrayElement(var contents: Array[String]) extends Element
From my understanding Scala compiler will generate contents , contents_= method for us. hence we can avoid to define def contents abstract method in base class. is it my understanding correct ? if it yes
val names = Array("welcome", "apple", "Test")
val names1 = Array("apple", "Test")
var t = new ArrayElement(names);
println(t.contents.mkString(",")) //Op : welcome,apple,Test
t.contents = names1 // because of var
#1 println(t.contents.mkString(",")) // op : apple,Test #2 println(t.lenth) // here i am getting 3 (length of names) . but i am expecting 2 . Why ? #3 println(t.maxLength) // same names reference here . Why ?
'#1 is giving updated names list. But #2 and #3 is giving old name references. Why ?