Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protected method that takes an abstract super class instance and the "access to protected method not permitted" error

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.

like image 722
Jude Gao Avatar asked Jan 05 '20 01:01

Jude Gao


People also ask

Can abstract class have protected methods?

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.

Can we override protected abstract method in Java?

Yes, the protected method of a superclass can be overridden by a subclass.

Can a subclass access protected superclass?

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.


1 Answers

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)
  }
like image 191
asanand Avatar answered Oct 25 '22 10:10

asanand