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.
Yes, you can declare an abstract method protected. If you do so you can access it from the classes in the same package or from its subclasses.
Yes, the protected method of a superclass can be overridden by a subclass.
Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
In scala, protected is more restrictive than in java.
I found this answer very helpful while understanding protected modifier for scala.
if protected member is not qualified(i.e. its just plain protected) then it is visible with another instances of declaring class into declaring class as well as with
this
into class and subclass.
So you could modify your addition function in SubclassSquare
as follows:
override def addition(another: AbstractSuperClass, num: Int): Int =
someFunction(num) + another.asInstanceOf[SubclassSquare].someFunction(num)
This code would compile, but it’d crash if the argument is not an instance of SubclassSquare
, so it’s not a good solution.
The better way is to use access qualifiers. So you just need to specify your package name with protected modifier e.g. protected[yourPackageName]
. Doing this will make your protected member accessible in classes in declared package or below. So your code will look like:
abstract class AbstractSuperClass {
protected[yourPackageName] def someFunction(num: Int): Int
def addition(another: AbstractSuperClass, num: Int): Int
}
class SubclassSquare extends AbstractSuperClass {
override protected[yourPackageName] def someFunction(num: Int): Int = num * num
override def addition(another: AbstractSuperClass, num: Int): Int =
someFunction(num) + another.someFunction(num)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With