Home » Developer & Programmer » JDeveloper, Java & XML » No modifier vs Protected (Java SE 1.7)
No modifier vs Protected [message #648980] Wed, 09 March 2016 05:33
Messages: 44
Registered: January 2011
Location: india

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();

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.


Previous Topic: Error while connecting with oci8 driver to default database: java.lang.NullPointerException
Next Topic: XML structure into regular table
Goto Forum:

Current Time: Sat Dec 16 16:56:52 CST 2017

Total time taken to generate the page: 0.08746 seconds