Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Java Interface Inner Class Protected Method is accessible from outside?

I have following code:

public interface ISomeInterface {

   class A {
      protected static void protectedMethod() {
         System.out.println("protectedMethod");
      }
      private static void privateMethod() {
         System.out.println("privateMethod");
      }

   }
}

In main, I can call protectedMethod but not privateMethod. Why is that?

public class Main {
   public static void main(String[] args) {

      ISomeInterface.A.protectedMethod(); //okay!
      //ISomeInterface.A.privateMethod(); // Compilation error
   }
}   

Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As per documentation:

"The protected keyword in Java is an access modifier used for member variables and methods. It provides a level of access control that allows access within the same package and by subclasses, even if they are in different packages."

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading