For example, the access rights modifier in Java _java

Source: Internet
Author: User
Tags modifier

Access permission characters:
(1) Public:
For members: Any other class can access them, whether in the same package or in another package.
For a class: it's the same.
(2) Friendly:
For members of the old saying: If a member of a class does not have any permissions to modify, then its door is the default package access, expressed in friendly, note

Italian friendly is not a keyword in Java, it is a way that people like to use it. Other classes in the same package can be accessed, but outside the package

It's not possible. For the same folder, there is no use of package Classes,java will automatically take these classes as a subordinate to the directory

Default package, you can invoke friendly members of class in each other. Two files in the same folder as the following two class

, although package is not introduced, it is subordinate to the same default package.

Class sundae{
//The following two methods default to friendly
Sundae () {}
Void f () {System.out.println ("sundae.f ()");
Public
class icecream{public
static void Main (string[] args) {
Sundae x = new Sundae ();
X.F ();
}
}

For a class: A class in the same package can be used. In short, a class can only be declared as public or friendly.
(3) Private:
For a member, it can only be accessed in the class that the member belongs to.

Class sundae{
Private Sundae () {}//can only be invoked in Sundae class
sundae (int i) {}
static Sundae Makasundae () {
    return new Sundae ();
}
public
class icecream{public
static void Main (string[] args) {
//Sundae class constructor Sundae (   is private,
//So it cannot be initialized with
//sundae x = new Sundae ();
Sundae y = new sundae (1);//sundae (int) is friendly, where you can invoke
Sundae z = Sundae.makasundae ();
}
}

For a class: A class cannot be declared private.

(4) Protected:
For members: Classes in the same package can be accessed (package access rights); The base class assigns the access rights of the members in the base class to the derived class (access rights for derived classes) by protected.

(5) Default permissions
class, data members, constructors, method members, can use default permissions, that is, no keywords are written. The default permissions are the same package permissions, and elements of the same package permission can only be invoked in the class in which they are defined, and in the class of the same package.

Example: Package c05.local;

Import Pack1. Cookies;
Note: Here Chocolatechip inherits the class cookie, and the bite () method is also
//chocolatechip, which can be called directly with X.bite, but not because the class chocolatechip
/ /And the cookie class is not in a package and each has package access, in order to be able to use X.bite () You must
change the access rights of the//cookie method to public or protected, but once you've changed to public all the people
can be accessed so as not to reach the privacy requirements, so set the protected best, both smooth access, can
//avoid outside class calls, protect good privacy role public
class Chocolatechip extends Cookie {public
 
  chocolatechip () {
   System.out.println ("Chocolatechip constructor");
  }
  public static void Main (string[] args) {
   chocolatechip x = new Chocolatechip ();
   X.bite (); Can ' t access bite
  
  }
}///:~
Package pack1;

public class Cookie {public
cookie ()
{
System.out.println ("Cookie constructor");
}

protected void bite () {System.out.println ("bite");}




For a class: A class cannot be declared as protected

For the permission modifiers for a class, here's a better explanation:

Access permissions for class classes:
Public: Can be accessed by all classes.
Default: The default can be called friendly but there is no friendly this modifier in the Java language, so the salutation should come from C + +. The default access rights are package-level access.
         that is, if a class is written without a write access modifier, then the default access permission is available to the class under the same package, even if the class can be instantiated
         (unless, of course, the class does not have the ability to instantiate, for example, the class does not provide a public constructor).

Description
1. Each compilation unit (class file) can have only one public class
2, the name of the public class (including case) must have the same names as their class files.
3, a class file (*.java) can not exist public class.
This form of existence scenario: if we write a class within a package, just to work with other classes within the package, and
We do not want to be bothered by the fact that writing a document to a customer (not necessarily a realistic customer, possibly a class that invokes this class), and it may take some time
It is possible to radically change the original practice and discard the old version entirely, replacing it with a new version.
4, class can not be private and protected.
5. If you do not want that any object that produces a class, you can set all constructors of that class to private. But even this can generate objects of that class, which are static members of Class (properties and methods).

Comprehensive Example:
First.java:

Package number; 
Import test.*; 
 
public class Frist extends Test 
{ 
protected String s1 = "Hello"; 
public static void Main (string[] args) 
{ 
String s2 = "java"; 
System.out.println (S1); 
SYSTEM.OUT.PRINTLN (S2); 
 
Frist t = new Frist (); 
System.out.println (T.S); 
T.show (); 
return; 
} 
 
Test.java: 
package Test; 
 
 
The public class Test  
{ 
protected String s = "Hello Test";//Can be accessed by classes in the same package and by subclasses, which can be different from the package Test public 
void Show () 
{ 
Test1 t1 = new Test1 (); 
return; 
} 
 
Class Test1 
{ 
Test1 () 
{ 
Test t = new test (); 
System.out.println (T.S); 
} 
 

Output:

Java 
Hello test 
Hello Test 

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.