5

Is there any security/access difference when making a package access level abstract class's non-static methods public vs making them protected? Only classes from within the same package that extend the abstract class can access the non-static methods anyway right? So, does it matter whether those non-static methods are public or protected since the abstract class itself places restrictions on who can extend it?

abstract class MyClass {
    protected void myFunction(){
        System.out.println("Only child classes can print this");
    }
}

abstract class MyClass {
    public void myFunction(){
        System.out.println("Still, only child classes can print this");
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • 2
    I don't think it is. I'm pretty sure that I understand the difference between them based on the definitions found in Oracle docs and that my question is regarding a specific case where the class' access levels render a methods access levels meaningless (aside from private). I might be misunderstanding it though. –  Dec 14 '15 at 06:09

1 Answers1

9

The public abstract method will be accessible in the other package where as the protected abstract method can not be accessed. Check the example below.

An abstract class with both public and protected abstract methods

package package1;

public abstract class MyClass {
  abstract protected String method1();
  abstract public String method2();
}

Another package which extends the class and implements the abstract class.

package package2;

import package1.MyClass;

public class MyClassImpl extends MyClass {
  @Override
  protected String method1() {
    return "protected method";
  }

  @Override
  public String method2() {
    return "public method";
  }
}

Main class for accessing the abstract method.

package package2;

import package1.MyClass;

public class MainClass {
  static MyClass myClass = new MyClassImpl();

  public static void main(String[] args) {
    System.out.println(myClass.method1());   // This is compilation error.
    System.out.println(myClass.method2());
  }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • There's a slight difference with your example. Your abstract class is `public`. Whereas, my abstract class has a "no modifier" access level. So, it can't be extended by classes outside of it's package anyway. –  Dec 14 '15 at 06:22
  • 2
    @Kyle Yes, That is true. It will not be accessible outside the defined package. Then protected or public method does not even come in the picture, because it can not be extended outside package. – YoungHobbit Dec 14 '15 at 06:26
  • Revisiting this question, I see that my comment is irrelevant. It doesn't matter that your example has a parent class that is public. Even with a no-modifier abstract class, the implementation class can still be public and reside in the same package as the abstract class and that means any other class would still be able to call the abstract class' public method through the public implementation class. So you were already completely right. Again, my comment was irrelevant lol –  Sep 19 '19 at 17:26