I made lowerBound method in my BinarySearchTree.
BinarySearchTree extends TreeMap[Int, Int].
So I made lowerBound method in BinarySearchTree.
but compiler said
treetest.scala:85: error: value lowerNeighbor is not a member of TreeMap[Int,Int] t2.lowerNeighbor(3)
How to made it? 🙂
class BinarySearchTree(private val root: Node) extends TreeMap[Int, Int] {
def lowerNeighbor(x : Int) : Int = {
var t = root
.........
}
}
var t2: TreeMap[Int, Int] = new BinarySearchTree
t2.lowerNeighbor(3)
You have declared your
t2variable to be of the static typeTreeMap[Int, Int]. Therefore, for the compiler, each time you uset2, it will assume it is an instance ofTreeMap[Int, Int].lowerNeighboris not a method defined onTreeMaps, but onBinarySearchTrees. The static type of your variable has to beBinarySearchTreeif you want to call thelowerNeighbormethod.** This is ignoring implicit conversions, which you may want to read up on once you’ve figured out the static type vs. dynamic type issue.