Can't access a method protected by ResizableArray, swap, from a subclass of ArrayBuffer?

ArrayBuffer extends ResizableArray, which includes a protected method, swap. But I can’t access the swap. What am I missing?

scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> val x=new ArrayBuffer[Int]() x: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> x+=3 scala> x+=5 scala> x.swap(0,1) <console>:7: error: method swap cannot be accessed in scala.collection.mutable.ArrayBuffer[Int] x.swap(0,1) ^ 
+1
scala
Oct 31 '09 at 15:10
source share
1 answer

According to this documentation , swap declared as a protected method, which means that although you can access it in ArrayBuffer code (or any other class derived from ResizableArray ), you cannot access it from another class.

From Scala Language Specification , p. 57:

The protected modifier is applied to the definition of class members. protected class members can be accessed from the inside
- template of the defining class,
- all templates that have a defining class as a base class,
- a companion module to any of these classes.

You do not have any of these contexts, so you see an error.

+4
Oct 31 '09 at 15:20
source share



All Articles