I contrived a fairly trivial example to illustrate my point as follows:
abstract class AbstractSuperClass {
protected def someFunction(num: Int): Int
def addition(another: AbstractSuperClass, num: Int): Int
}
class SubclassSquare extends AbstractSuperClass {
override protected def someFunction(num: Int): Int = num * num
override def addition(another: AbstractSuperClass, num: Int): Int =
someFunction(num) + another.someFunction(num)
}
I received the following error message upon execution of the code. (of course, the main
function is properly defined.)
Error:(12, 33) method someFunction in class AbstractSuperClass cannot be accessed in AbstractSuperClass Access to protected method someFunction not permitted because prefix type AbstractSuperClass does not conform to class Subclass where the access takes place someFunction(num) + another.someFunction(num)
The method addition
is causing the problem. The code tries to access the protected field of an AbstractSuperClass
instance, but from my perspective, it should cause no issue here since the SubclassSquare
is a sub-class of AbstractSuperClass
.
However, I know there must be something I do not understand here. I would like to know how to change my code to make it compile. Well-appreciated.