Java Access Controller

Source: Internet
Author: User
For Class: Default: it can be accessed from other classes in the same package and inherited from other classes in the same package. Public: can be accessed and inherited from any class in any package. // Render a class visible to other parts of the program as a whole. It does not represent that all the fields and methods in the class are visible to other parts of the program, the former is a necessary condition for the latter. Whether the domain and method of the class can be accessed (including inheritance) for all other classes depends on the access controllers of these domains and methods. // If the constructor of a class is declared as private, other classes cannot generate an instance of this class. // Although the subtype in different packages can be used to access the members of the parent class limited to protected, the objects accessing these members must have the subclass type or the subclass type, it cannot be a parent class. // Configure //---------------------------------------------------------------------------------------------------------------------
  In the same class In the same package Subclass of different packages Non-subclass of different packages
Private Allow      
Default Allow Allowed (subclass and non-subclass)    
Protected Allow Allowed (subclass and non-subclass) Allowed (only subclass, and the protected method inherited by subclass cannot be accessed through the subclass instance) Unable to access the protedted Method
Public Allow Allowed (subclass and non-subclass) Allow Allowed (subclass and non-subclass)
For Class Members:// The Secret subclass can inherit the "default" Domain variables and methods set to public and protected in the parent class, but cannot inherit the domain variables and methods with private access permissions. // ----------------------------- The access modifier and non-access modifier that cannot be used simultaneously: abstract and final cannot simultaneously modify the same class; abstract and private, static, final or native cannot be used together to modify the same method; Private-modified domains and methods cannot be used in abstract classes; abstract methods must exist in abstract classes; the static method cannot process non-static fields. Sample Code: Package pkg1;
Public class country
{
Protected string name;
Protected void value () // friendly
{
Name = "China ";
}
} // ------------------------ Package pkg2;
Import pkg1.country;
Public class main6
{
Public static void main (string [] ARGs)
{
Country C = new country ();
// C. Value (); // Error
}
}
// Sub-classes can inherit the permissions set to public, protedted, and friedly (default) in the parent class.
// Domain variables and methods, but cannot inherit the domain variables and methods with the access permission set to private
Class city extends country
{
Void setvalue ()
{
This. Value (); // correct
Super. Value (); // correct
Country C = new country ();
// C. Value (); // error: the object type accessing the protected member cannot be the parent class.
City cc = New City ();
Cc. Value (); // correct
}
}

 

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.