1, Java has four access modifiers: default/public/private/protected
2, they have different access rights, and in different situations and have different access rights, divided into the following several
{
1, the same kind of
{
First we write a piece of code to test
{
Class Boar
{
void Defmethod ()
{
System.out.println ("default method");
}
protected void Promethod ()
{
System.out.println ("protected Method");
}
private void Primethod ()
{
System.out.println ("Private Method");
}
public void Pubmethod ()
{
System.out.println (' public method ');
}
public static void Main (string[] sl)
{
Boar objboar=new boar ();
Objboar.defmethod ();
Objboar.promethod ();
Objboar.primethod ();
Objboar.pubmethod ();
}
Save, compile, succeed.
Print results:
Default method
Protectedmethod
Privatemethod
Publicmethod
Through the above tests, we conclude that these four methods are accessible in the same class.
}
}
2, two classes in the same package
{
First we write the code (write two classes and put them under the same package):
{
Class One:
package boar.medemo;
Class Boar
{
void Defmethod ()
{
System.out.println ("default method");
}
protected void Promethod ()
{
System.out.println ("protected Method");
}
private void Primethod ()
{
System.out.println ("Private Method");
}
public void Pubmethod ()
{
System.out.println (???);
}
Class II:
package boar.medemo;
Class Boardemo
{public
static void Main (string[] sl)
{
Boar objboar=new boar ();
Objboar.defmethod ();
Objboar.primethod ();
Objboar.promethod ();
Objboar.pubmethod ();}}
Save the Code, compile, at this time, the compiler will prompt the following error prompts:
Boardemo.java:8: Primethod () can access private in Boar.medemo.boar
Objboar.primethod ();
^
1 Error
It means that the Primethod () method in the Boar method cannot access
So we conclude that the private method has no access to the same package.
}
3, in different packets of the neutron class to continue the parent class
{
First, let's write code (write two classes, put them in different packages)
{
Code one:
Packageboar.medemo;
public class boar
{
void Defmethod ()
{
System.out.println (' default method ');
}
protected void Promethod ()
{
System.out.println ("protected Method");
}
private void Primethod ()
{
System.out.println ("Private Method");
}
public void Pubmethod ()
{
System.out.println (???);
}
Code two:
import Boar.medemo.boar;
Class Boardemo extends boar
{public
void Pro ()
{
defmethod ();
Primethod ();
Promethod ();
Pubmethod ();}}
Save, compile (javac-d. Boar.java and javac-d. Boardemo.java), the system prompts:
Boardemo.java:6: Symbol not found
Symbols: Method Defmethod ()
Location: Class Boardemo
Defmethod ();
^
Boardemo.java:7: Symbol not found
Symbols: Method Primethod ()
Location: Class Boardemo
Primethod ();
^
2 Error
We can see that in different packages, the default and private methods are inaccessible. Because the Boardemo class is inherited from the boar class, the protected method can also be accessed. The default method, which we have already validated, can only be accessed by classes in the same package, so the default method cannot be accessed
So we get: in different packages (inherited), the default method (default) and private method (private) are inaccessible
}
}
Diagram:
Private class can not be the same as the default class + package in different packages in the import can not be protect class + within package + inherit public