package access vs public constructors

本文深入探讨Java中不同访问修饰符对类成员可见性的影响,包括public、protected、默认(包访问)和private的使用场景及限制。通过实例说明如何正确设定类成员的访问级别,以实现良好的封装性和模块间协作。

1. package access class with public constructor

// hiding/packageaccess/PublicConstructor.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package hiding.packageaccess;

class PublicConstructor {
  public PublicConstructor() {}
}

2. access public construtor above

// hiding/CreatePackageAccessObject.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {WillNotCompile}

import hiding.packageaccess.*;

public class CreatePackageAccessObject {
  public static void main(String[] args) {
    new PublicConstructor();// compile error
  }
}

my compiler error message:

CreatePackageAccessObject.java:7: error: package hiding.packageaccess does not exist
import hiding.packageaccess.*;
^
CreatePackageAccessObject.java:11: error: cannot access PublicConstructor
    new PublicConstructor();
        ^
  bad source file: ./packageaccess/PublicConstructor.java
    file does not contain class PublicConstructor
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors

Summary, declaring a constructor public inside a packageaccess class doesn't actually make it public, and it should probably be flagged with a compiler error at the point of declaration.

The following table shows the access to members permitted by each modifier.

Access Levels
ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN

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.

Classes and Packages of the Example Used to Illustrate Access Levels

 

Classes and Packages of the Example Used to Illustrate Access Levels

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.

Visibility
ModifierAlphaBetaAlphasubGamma
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN

Note the difference in column names between table Access Levels and table Visibility.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/hiding/packageaccess/PublicConstructor.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/hiding/CreatePackageAccessObject.java

4. https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值