Summary: difference between public, default, protected, and private key words
2015-01-29 14:20
369 查看
According to Java Tutorial:
At the top level—
At the member level—
A class may be declared with the modifier
At the member level, you can also use the
The following table shows the access to members permitted by each modifier.
Access Levels
The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.
Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.
Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.
Java Array sort descending order:
Controlling Access to Members of a Class
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:At the top level—
public, or package-private (no explicit modifier).
At the member level—
public,
private,
protected, or package-private (no explicit modifier).
A class may be declared with the modifier
public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)
At the member level, you can also use the
publicmodifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers:
privateand
protected. The
privatemodifier specifies that the member can only be accessed in its own class. The
protectedmodifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
The following table shows the access to members permitted by each modifier.
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.
Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.
Java Array sort descending order:
sort(T[] a, Comparator<? super T> c)
Arrays.sort(a, Collections.reverseOrder());
相关文章推荐
- Java Difference between Private and Protected
- Java Difference between Private and Protected
- Public, Private, Protected, and default identifiers
- The difference between the private and public clouds
- In Java, what's the difference between public, default, protected, and private?
- Java中的private、protected、public和default的区别
- Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class
- public protected default private 权限
- Java 中访问修饰符public,private,protected和default(friendly) 的区别
- 继承及java作用域public ,private ,protected, default
- Java中的public、protected、default和private的区别
- Java中private、protected、public和default的区别
- Self Summary: Lua Language(two useful links) and the difference between : and .
- Java中的private、protected、public和default的区别
- Java中的修饰符区别(public,private,protected,default, final)
- java中访问权限控制public protected default(friendly) 和private
- java中的访问修饰符--public、protected、default、private
- JAVA修饰词详解:public、protected、default、private
- java 修饰符public protected default private
- Java中的private、protected、public和default的区别