2

I am a beginner in Java and have just started writing core java codes. I went through the access control table in the page : https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

According the table, for a subclass, access to method of the Super class is restricted IF the method does not have a modifier (lets call this CASE-1).

If the method is declared using "protected", then access is granted to the sub class (lets call this CASE-2).

I prepared the following codes to test CASE-1 and CASE-2. Super class : Test_1 code :

package com.nc.test;

public class Test_1 {
    protected void test_3_print() 
    {
      System.out.println("Test 3 print.");
    }
    public static void main(String[] args) {
      // TODO Auto-generated method stub
      System.out.println("Test 1 MAIN CLASS.");
    }
}

Sub class : subTest_1 code:

package com.nc.test1;
import com.nc.test.Test_1;
class subTest_1 extends Test_1 {

  public static void main(String[] args) 
  {
    System.out.println("FROM SUB CLASS.");
    Test_1 t11 = new Test_1();
    t11.test_3_print();
  }
}

Method being tested : test_3_print

When test_3_print does not contain any modifier ( only void..this is CASE-1 ), then I encounter the error :

"The method test_3_print() from the type Test_1 is not visible"

This is in accordance with the table I mentioned above. But, when the modifier of test_3_print is set to "protected" as it is in the above code, I still get the same error. This is contradicting with the table.

Am I doing something wrong or am I missing something?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sinpeak
  • 21
  • 3
  • what make `subTest_1` a subclass of `Test_1`? I don't see the `extends` keyword – jack3694078 Mar 11 '16 at 06:14
  • Sorry. I have now updated the snippet in the question . Kindly refer again. Thanks. – sinpeak Mar 11 '16 at 07:02
  • Still the same error with this code? You should better post the real code you use for testing – jack3694078 Mar 11 '16 at 07:11
  • Yes. Still same error. I have again updated my question with the exact code that is giving me the error. I also see that if I make test_3_print() static, then the code works fine. So, if it is about "static" method being called from a static context ( main function being static ), then I guess the error message is not right OR may be I am doing something wrong OR missing something. Please advise. – sinpeak Mar 11 '16 at 08:09
  • Oh I see it now. It is because you're trying to call that method via `Test_1` instead of `subTest_1` and they are in the different packages. Read [this post](http://stackoverflow.com/questions/5562548/protected-member-access-from-different-packages-in-java-a-curiosity) that asked about the same thing. – jack3694078 Mar 11 '16 at 08:43
  • 1
    [Here's a more clear version of that table.](http://stackoverflow.com/questions/215497/difference-between-public-default-protected-and-private/33627846#33627846) Have a look at the column that says "different package but subclass". In this particular case, you're trying to invoke `test_3_print` from `subTest_1` when it is declared in `Test_1`. This is not ok in a static context. From JLS 6.6.2: *A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.* – aioobe Mar 11 '16 at 09:05

0 Answers0