Four access modifiers in JAVA (by independent synthesis) __java

Source: Internet
Author: User
Tags modifiers

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.