在Java访问受保护的成员
在Java,有四种类型的访问修饰符。它们是公共的、私有的、默认的和受保护的。要了解这些修饰符,您可以参考Java的访问修饰符。在本文中,我们将讨论不同情况下受保护成员的可访问性。
现在让我们讨论访问受保护成员的各种场景,如下所示:
- 在同一班级访问
- 在同一个包的其他类中访问
- 访问同一包中子类中类的受保护成员
- 访问不同包中的另一个类
- 在不同包中的子类中访问
案例一:访问同一个类中的protected成员
We can access protected members of a class anywhere in it.
例子:
Java
// Java Program to Illustrate
// Accessing Protected Members
// in the same class
// Main class
class Sample {
protected int year = 2021;
protected void printYear()
{
System.out.println("Its " + year + " !!");
}
public static void main(String[] args)
{
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
Java
// Java Program to Illustrate Accessing
// Protected Members
// In Other Class of Same Package
// Class 1
class Sample {
protected int year = 2021;
protected void printYear() {
System.out.println("Its "+year+" !!");
}
}
// Class 2
public class Test {
// Main driver method
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
Java
// Java Program to Illustrate
// Accessing Protected Members
// of a class in its subclass
// in the same package
// Class 1
class Sample {
static protected String title = "geekforgeeks";
protected int year = 2021;
protected void printYear() {
System.out.println("Its "+year+" !!");
}
}
// Class 2
public class Test extends Sample {
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}
Java
// Java Program to Illustrate
// Accessing Protected Members
// In Another Class in a
// Different Package
// Package 1
package package1;
// Main class
public class Sample {
static protected String title = "geeksforgeeks";
protected int year = 2021;
// Protected method
protected void printYear() {
System.out.println("Its "+year+" !!");
}
}
Java
// Java Program to Illustrate
// Accessing Protected Members
// In Another Class in a
// Different Package
// Package 1
package package2;
// Importing above package
import package1.Sample;
// Main class
public class Test {
// Main driver method
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}
Java
// Java Program to Illustrate Accessing Protected
// Members in sub-class in a different package
package package1;
// Class
public class Sample {
// Protected attributes
static protected String title = "geeksforgeeks";
protected int year = 2021;
protected void printYear()
{
System.out.println("Its " + year + " !!");
}
}
Java
// Java Program to Illustrate Accessing Protected
// Members in Sub-class in a different Package
package package2;
// Importing class from above package
import package1.Sample;
// Main class
public class Child extends Sample {
// Method 1
void helper()
{
System.out.println(year);
printYear();
System.out.println(Sample.title);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating child class instance inside main()
Child child = new Child();
child.helper();
}
}
Java
// Java Program to Illustrate Compile-time Error Thrown
// When we are Trying to Access Protected Members
// Using Parent Class Reference
// Importing packages
package package2;
import package1.Sample;
// Importing class extending to Parent class
public class Child extends Sample {
// Method 1
void helper()
{
// Creating instance of Child class inside main()
Child myself = new Child();
// As Child is sub-class of Sample, we can access
// the static variable here
System.out.println(Sample.title);
// This will compile fine as we are accessing year
// using child class reference variable
System.out.println(year);
System.out.println(myself.year);
// This will compile fine as we are accessing
// printYear() method using child class reference
// variable
printYear();
myself.printYear();
// Creating parent class object
Sample sample = new Sample();
// Parent class reference holding child class object
Sample child = new Child();
// Note: Below lines of code won't compile as we are
// trying to access protected members of parent
// class using Parent's class reference which is
// present in other package
// Errors will be thrown
System.out.println(sample.year);
sample.printYear();
child.printYear();
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating child class instance inside main()
Child child = new Child();
// Calling the above method 1 of child class
child.helper();
}
}
2021
Its 2021 !!
案例二:访问同包其他类中的protected成员
We can access protected members of a class in another class that is present in the same package.
Java
// Java Program to Illustrate Accessing
// Protected Members
// In Other Class of Same Package
// Class 1
class Sample {
protected int year = 2021;
protected void printYear() {
System.out.println("Its "+year+" !!");
}
}
// Class 2
public class Test {
// Main driver method
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
2021
Its 2021 !!
情况 3:访问同一个包中的子类中的类的受保护成员
We can access protected members of a class in its subclass if both are present in the same package.
例子
Java
// Java Program to Illustrate
// Accessing Protected Members
// of a class in its subclass
// in the same package
// Class 1
class Sample {
static protected String title = "geekforgeeks";
protected int year = 2021;
protected void printYear() {
System.out.println("Its "+year+" !!");
}
}
// Class 2
public class Test extends Sample {
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}
2021
Its 2021 !!
geekforgeeks
案例 4:访问不同包中另一个类中的受保护成员
We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.
示例 1:包 1
Java
// Java Program to Illustrate
// Accessing Protected Members
// In Another Class in a
// Different Package
// Package 1
package package1;
// Main class
public class Sample {
static protected String title = "geeksforgeeks";
protected int year = 2021;
// Protected method
protected void printYear() {
System.out.println("Its "+year+" !!");
}
}
示例 2:包 2
Java
// Java Program to Illustrate
// Accessing Protected Members
// In Another Class in a
// Different Package
// Package 1
package package2;
// Importing above package
import package1.Sample;
// Main class
public class Test {
// Main driver method
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}
输出:
error: year has protected access in Sample
System.out.println(sample.year);
^
error: printYear() has protected access in Sample
sample.printYear();
^
error: title has protected access in Sample
System.out.println(Sample.title);
^
它会给出编译时错误。在下面的示例中,我们将创建两个类。 package1 中的 Sample 类和 package2 中的 Test 类,并尝试访问 Test 类中 Sample 类的受保护成员。在上面的两个例子中是有道理的。
案例 5:访问不同包中子类中受保护的成员
我们可以访问存在于不同包中的子类中的类的受保护成员。在下面的示例中,我们将创建两个类。 package1 中的示例类和 package2 中的 Child 类。子类扩展了 Sample 类。
例 1.1
Java
// Java Program to Illustrate Accessing Protected
// Members in sub-class in a different package
package package1;
// Class
public class Sample {
// Protected attributes
static protected String title = "geeksforgeeks";
protected int year = 2021;
protected void printYear()
{
System.out.println("Its " + year + " !!");
}
}
例 1.2
Java
// Java Program to Illustrate Accessing Protected
// Members in Sub-class in a different Package
package package2;
// Importing class from above package
import package1.Sample;
// Main class
public class Child extends Sample {
// Method 1
void helper()
{
System.out.println(year);
printYear();
System.out.println(Sample.title);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating child class instance inside main()
Child child = new Child();
child.helper();
}
}
输出
2021
Its 2021 !!
geeksforgeeks
Note: From the above output it can be perceived we have successfully accessed the protected members directly as these are inherited by the Child class and can be accessed without using any reference. The protected members are inherited by the child classes and can access them as its own members. But we can’t access these members using the reference of the parent class. We can access protected members only by using child class reference.
示例 2
Java
// Java Program to Illustrate Compile-time Error Thrown
// When we are Trying to Access Protected Members
// Using Parent Class Reference
// Importing packages
package package2;
import package1.Sample;
// Importing class extending to Parent class
public class Child extends Sample {
// Method 1
void helper()
{
// Creating instance of Child class inside main()
Child myself = new Child();
// As Child is sub-class of Sample, we can access
// the static variable here
System.out.println(Sample.title);
// This will compile fine as we are accessing year
// using child class reference variable
System.out.println(year);
System.out.println(myself.year);
// This will compile fine as we are accessing
// printYear() method using child class reference
// variable
printYear();
myself.printYear();
// Creating parent class object
Sample sample = new Sample();
// Parent class reference holding child class object
Sample child = new Child();
// Note: Below lines of code won't compile as we are
// trying to access protected members of parent
// class using Parent's class reference which is
// present in other package
// Errors will be thrown
System.out.println(sample.year);
sample.printYear();
child.printYear();
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating child class instance inside main()
Child child = new Child();
// Calling the above method 1 of child class
child.helper();
}
}
输出
error: year has protected access in Sample
System.out.println(sample.year);
^
error: printYear() has protected access in Sample
sample.printYear();
^
error: printYear() has protected access in Sample
child.printYear();
^
所以默认访问修饰符和受保护修饰符的主要区别在于默认成员只能在当前包中访问。而受保护的成员只能在其子类中访问同一包和包外的任何位置,并且只能使用子类的引用变量,而不是父类的引用变量。我们无法使用父类的引用访问受保护的成员。