No modifier vs Protected [message #648980] |
Wed, 09 March 2016 05:33 |
|
sinpeak
Messages: 59 Registered: January 2011 Location: india
|
Member |
|
|
Hello,
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.(..and 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 {
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...and this is contradicting with the table.
Am I doing something wrong ? or missing something ?
Please advise.
Thanks.
|
|
|