Java uses modifiers to control access to classes, properties, and methods, and other functions, usually at the forefront of the statement. For example:
1 public class ClassName {2 //Body of CLASS3}4 private boolean myflag;5 static final double weeks = 9.5;6
protected static final int boxwidth = 42;7 public static void main (string[] arguments) {8 //Body of METHOD9}
Java has many modifiers, including access modifiers and non-access modifiers. This section describes only the access modifiers, and the non-access modifiers are described later.
Access modifiers, also called access controls, are keywords that control the use of classes, member variables, and methods.
In object-oriented programming, access control is an important concept that can be used to protect access to classes, variables, methods, and construction methods.
Java supports four different kinds of access rights:
modifier |
Description |
Public |
Common, visible to all classes. |
Protected |
Protected, visible to classes and all subclasses within the same package. |
Private |
Private, visible within the same class. |
The default |
Visible within the same package. No modifiers are used by default. |
Public: Common
Classes, methods, construction methods, and interfaces that are declared public can be accessed by any other class.
If several publicly accessed public classes are distributed in unused packages, you need to import the package that contains the appropriate public class. Because of the inheritance of classes, all public methods and variables of a class can be inherited by their subclasses.
The following methods use public access control:
public static void Main (string[] arguments) { //body of method}
The main () method of the Java program must be set to public, otherwise the Java interpreter will not be able to run the class.
Protected: Protected
Variables, methods, and construction methods declared as protected can be accessed by any other class in the same package, or by subclasses in different packages.
The protected access modifier cannot decorate classes and interfaces, and methods and member variables can be declared as protected, but the member variables and member methods of an interface cannot be declared as protected.
Subclasses can access the methods and variables declared by the protected modifier, which protects unrelated classes from using these methods and variables.
The following parent class uses the protected access modifier, and the subclass overloads the Bark () method of the parent class.
1 public class dog{2 protected void bark () {3 System.out.println ("bark, don't come Over"); 4 } 5} 6 class Teddy extends D og{ //Teddy 7 void bark () {8 System.out.println ("Bark, I'm afraid, don't Follow Me"); 9 }10}
If the bark () method is declared private, a class other than dog will not be able to access the method. If you declare bark () as public, all classes have access to the method. If we only want the method to be visible to subclasses of its class, declare the method as protected.
Private: Privately-owned
Private access modifiers are the most restrictive access level, so methods, variables, and construction methods that are declared private can only be accessed by the owning class, and classes and interfaces cannot be declared private.
A variable declared as a private access type can only be accessed by an external class through the public Getter/setter method in the class.
The use of the private access modifier is primarily used to hide the implementation details of the class and to protect the class's data.
The following class uses the private access modifier:
1 public class dog{2 private string name, 3 private int age, 4 public String GetName () {5 return name; 6 } 7 public void SetName (String name) {8 this.name = name, 9 }10 public int Getage () {One return a ge;12 }13 public void Setage (int.) { this.age = age;15 }16}
In the example, the name and age variables in the dog class are private variables, so other classes cannot directly get and set the value of the variable. To enable other classes to manipulate the variable, the two-to-public method is defined, GetName ()/setname () and Getage ()/setage (), which are used to get and set the value of the private variable.
This is a keyword in java.
The method of defining access to a private variable in a class is customarily named by adding "get" or "set" in front of the variable name and capitalizing the first letter of the variable. For example, the method that gets the private variable name is GetName (), and the method for setting name is SetName (). These methods are often used, and also have specific salutation, called Getter and Setter methods.
Default: Do not use any keywords
Properties and methods declared with no modifiers are visible to classes within the same package. The variables in the interface are implicitly declared as public static final, and the methods in the interface are public by default.
As the following example shows, the definitions of classes, variables, and methods do not use any modifiers:
1 class dog{2 String name; 3 int age 4 5 void bark () { //bark 6 System.out.println ("bark, don't come Over"); 7 } 8 void Hungry () { //Hunger 9 System.out.println ("Master, I'm Hungry"); }11}
Access control and inheritance
Note the following methods inherit the rule:
Methods declared as public in the parent class must also be public in the subclass.
A method declared as protected in a parent class is either declared as protected in the subclass or declared as public. cannot be declared as private.
The method declared by the default modifier in the parent class, which can be declared as private in the subclass.
The method declared as private in the parent class cannot be inherited.
How to use the access control character
Access control allows us to easily control the permissions of the code:
- You can declare the access control of a class as public when you need to have the class you write accessed by all other classes.
- You can omit the access control when you need to make your class accessible only to classes in your own package.
- When you need to control member data in a class, you can set the member data access control in the class to public, protected, or omit it.
Series Articles:
Java know how much (top)
Java know how much (medium)
Java know how much (bottom)
From:http://www.cnblogs.com/coda/p/4376323.html
Java know how many (19) access modifiers (access control characters)